 <p>Seems like a job for data structure called <code>multiset</code>.</p> <pre><code>Multiset&lt;Integer&gt; mp = HashMultiset.create(); mp.addAll(Arrays.asList(new Integer[] { 3, 3, 3, 1, 5, 8, 11, 4, 5 })); </code></pre> <p>Standard JDK 6 is primitive and do not contain <code>multiset</code>. If you do not want to rewrite it, you can use preexisting library like Google Guava-libraries or Apache Commons. </p> <p>For example with Guava-libraries you can</p> <pre><code> for (Integer i : mp.elementSet()) System.out.println(i + " is contained " + mp.count(i) + " times."); </code></pre> <p>And this would output:</p> <pre><code>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. </code></pre>