Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentifying duplicate elements in a list containing 300k+ Strings
    primarykey
    data
    text
    <p>I have a list containing 305899 Strings (which is the username for a website). After I remove all the duplicates, the number goes down to 172123 Strings.</p> <p>I want to find how many times a particular String (the username) is repeated in that ArrayList. I wrote a simple bubble sort type logic but it was too slow. </p> <pre><code>private static Map&lt;String, Integer&gt; findNumberOfPosts(List&lt;String&gt; userNameList) { Map&lt;String, Integer&gt; numberOfPosts = new HashMap&lt;String, Integer&gt;(); int duplicate = 0; int size = userNameList.size(); for (int i = 0; i &lt; size - 1; i++) { duplicate = 0; for (int j = i + 1; j &lt; size; j++) { if (userNameList.get(i).equals(userNameList.get(j))) { duplicate++; userNameList.remove(j); j--; size--; } } numberOfPosts.put(userNameList.get(i), duplicate); } return numberOfPosts; } </code></pre> <p>Then I changed it to this:</p> <pre><code>private static Map&lt;String, Integer&gt; findNumberOfPosts(List&lt;String&gt; userNameList) { Map&lt;String, Integer&gt; numberOfPosts = new HashMap&lt;String, Integer&gt;(); Set&lt;String&gt; unique = new HashSet&lt;String&gt;(userNameList); for (String key : unique) { numberOfPosts.put(key, Collections.frequency(userNameList, key)); } return numberOfPosts; } </code></pre> <p>This was really slow as well. When I mean slow, it would take like 30+ minutes to through the list. </p> <p>Is there any other efficient way to handle this problem? Just reduce the time it takes to find and count duplicate elements?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload