Remove duplications from a Java String today I tackled this coding challenge: Remove duplications from string.
I was thinking of either using a Hashmap or a Set but realized that a set might have a longer run time since the way it determines if a numbers is in there or not is by traversing each element. This lead me to use a hashmap instead. I have not taken care of any edge cases but wanted to get people's opinion of this implementation. From what I understand this would be a \$O(n)\$, (\$n\$ being the length of the input String), correct?

  String test = "Banana";
    HashMap&lt;Character, String&gt; nodups = new HashMap&lt;&gt;();
    for(int i = 0; i &lt; test.length();i++){
        nodups.put(test.charAt(i),String.valueOf(test.charAt(i)));
    }

    StringBuilder noDupsString = new StringBuilder();
    for(Character key : nodups.keySet()){
        noDupsString.append(nodups.get(key));
    }

    System.out.println("String value with no dups :" + noDupsString);
}
Soo.. let me tell you a secret about Java's HashSet...


  This class implements the Set interface, backed by a hash table (actually a HashMap instance)


to cut the chase short: you could've just used a HashSet, it would've been at worst equally slow.

now to review the code... Since you don't make any guarantees for execution order in your code, neither will I:

System.out.println("String value with no duplicates: " 
    + new HashSet&lt;&gt;(Arrays.asList(test.toCharArray())).stream().collect(Collectors.joining("")));


It should be acceptably easy to see, that this is horrendous code, but it does the same thing as your code...

Either way, the code presented also has some issues:


Unnecessary creation of (relatively speaking) expensive objects.
String.valueOf(test.charAt(i)) is just bad form
Strangely hidden complexity.
In nodups.put(..) there's a hideous hidden complexity. You're hashing the character (which basically just turns it into an int and does some modulo calculations) and then put the char into it's place.
Double loop, where one is sufficient.
Currently the code does two loops, where one is sufficient. In a HashSet the contains-operation is \$\mathcal{O}(1)\$. This allows you to just collapse the loops instead of repeatedly overwriting the same value:

Set&lt;Character&gt; knownChars = new HashSet&lt;&gt;();
StringBuilder nodupsString = new StringBuilder();
for (Character c : test.toCharArray()) {
    if (!knownChars.contains(c)) {
        knownChars.add(c);
        nodupsString.append(c);
    }
}


This reduces the mathematical complexity from \$\mathcal{O}(2n)\$ to \$\mathcal{O}(n)\$. And don't come with the "constants are ignored in O-notation" fallacy. The problem is that too many people think that and as a result write badly performing code, which they think can't be made faster ... &lt;/rant>


Aside from that ... good job, I guess?