× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Identify duplicates in a List (with iterating the list only once)

Leif Gensert
Apr 01, 2016
<p>Say you have a list like this:</p> <pre><code><span style="color:rgb(46, 49, 51)">[</span><span style="color:rgb(107, 41, 27)">1</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">1</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">2</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">3</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">3</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">3</span><span style="color:rgb(46, 49, 51)">]</span></code></pre> <p>And you want to find all duplicates in a separate list. In this example:</p> <pre><code><span style="color:rgb(46, 49, 51)">[</span><span style="color:rgb(107, 41, 27)">1</span><span style="color:rgb(46, 49, 51)">,</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(107, 41, 27)">3</span><span style="color:rgb(46, 49, 51)">]</span></code></pre> <p>You can utilize the return value of the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Set.html#add%28E%29">Set#add method</a>, which returns true if the value already existed and false if it did not. This will lead you to the following code:</p> <pre><code><span style="color:rgb(0, 0, 139)">public</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(43, 145, 175)">Set</span><span style="color:rgb(46, 49, 51)">&lt;</span><span style="color:rgb(43, 145, 175)">Integer</span><span style="color:rgb(46, 49, 51)">&gt;</span><span style="color:rgb(46, 49, 51)"> findDuplicates</span><span style="color:rgb(46, 49, 51)">(</span><span style="color:rgb(43, 145, 175)">List</span><span style="color:rgb(46, 49, 51)">&lt;</span><span style="color:rgb(43, 145, 175)">Integer</span><span style="color:rgb(46, 49, 51)">&gt;</span><span style="color:rgb(46, 49, 51)"> listContainingDuplicates</span><span style="color:rgb(46, 49, 51)">)</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">{</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">final</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(43, 145, 175)">Set</span><span style="color:rgb(46, 49, 51)">&lt;</span><span style="color:rgb(43, 145, 175)">Integer</span><span style="color:rgb(46, 49, 51)">&gt;</span><span style="color:rgb(46, 49, 51)"> setToReturn </span><span style="color:rgb(46, 49, 51)">=</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">new</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(43, 145, 175)">HashSet</span><span style="color:rgb(46, 49, 51)">();</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">final</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(43, 145, 175)">Set</span><span style="color:rgb(46, 49, 51)">&lt;</span><span style="color:rgb(43, 145, 175)">Integer</span><span style="color:rgb(46, 49, 51)">&gt;</span><span style="color:rgb(46, 49, 51)"> set1 </span><span style="color:rgb(46, 49, 51)">=</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">new</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(43, 145, 175)">HashSet</span><span style="color:rgb(46, 49, 51)">();</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">for</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">(</span><span style="color:rgb(43, 145, 175)">Integer</span><span style="color:rgb(46, 49, 51)"> yourInt </span><span style="color:rgb(46, 49, 51)">:</span><span style="color:rgb(46, 49, 51)"> listContainingDuplicates</span><span style="color:rgb(46, 49, 51)">)</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">{</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">if</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">(!</span><span style="color:rgb(46, 49, 51)">set1</span><span style="color:rgb(46, 49, 51)">.</span><span style="color:rgb(46, 49, 51)">add</span><span style="color:rgb(46, 49, 51)">(</span><span style="color:rgb(46, 49, 51)">yourInt</span><span style="color:rgb(46, 49, 51)">))</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">{</span><span style="color:rgb(46, 49, 51)"> setToReturn</span><span style="color:rgb(46, 49, 51)">.</span><span style="color:rgb(46, 49, 51)">add</span><span style="color:rgb(46, 49, 51)">(</span><span style="color:rgb(46, 49, 51)">yourInt</span><span style="color:rgb(46, 49, 51)">);</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">}</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">}</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(0, 0, 139)">return</span><span style="color:rgb(46, 49, 51)"> setToReturn</span><span style="color:rgb(46, 49, 51)">;</span><span style="color:rgb(46, 49, 51)"> </span><span style="color:rgb(46, 49, 51)">}</span></code></pre>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus