It seems to work for a data structure called multiset .
Multiset<Integer> mp = HashMultiset.create(); mp.addAll(Arrays.asList(new Integer[] { 3, 3, 3, 1, 5, 8, 11, 4, 5 }));
The standard JDK 6 is primitive and does not contain multiset . If you do not want to rewrite it, you can use a pre-existing library such as Google Guava libraries or Apache Commons.
For example, using Guava libraries you can
for (Integer i : mp.elementSet()) System.out.println(i + " is contained " + mp.count(i) + " times.");
And this will output:
1 is contained 1 times. 3 is contained 3 times. 4 is contained 1 times. 5 is contained 2 times. 8 is contained 1 times. 11 is contained 1 times.
source share