May I know the algorithm to calculate how many different values are in the array?
May I know the algorithm to calculate how many different values are in the array?
Input array:{ac,a,b,a,a,ac,b,b,a,a}
Output:Ac=2,a=5,b=3
Total number of different values=3
Thanking you,
With Regards,
Nandhini
Re: May I know the algorithm to calculate how many different values are in the array?
To calculate how many distinct elements are in an array put the array elements into an instance of Set and look at its size. See the example in the The Set Interface page of Oracle's Tutorial.
To find out both the distinct elements *and* their frequencies you could use a Map which links each distinct element (as key) with its frequency (as value). For each element of the array you add an entry to the map or update an existing one: if the element is there already you increment the count, if not you create a new entry with a count of 1. Again, there is an example in the Tutorial's Collection on the page The Map Interface.
Re: May I know the algorithm to calculate how many different values are in the array?
Re: May I know the algorithm to calculate how many different values are in the array?