[Solved] Searching Duplicate String Complexity


You are making your code way too complicated, use a HashSet<String>, which will guarantee uniqueness and will return whether the element was already in the set.

public class DuplicateEle {
    public static void main(String args[]) {
      Set<String> seen = new HashSet<>();
      String[] arr = { "hello", "hi", "hello", "howru" };

      for (String word : arr) {
        boolean unique = seen.add(word);
        System.out.printf("%s is duplicate: %b%n", word, !unique);
      }
    }
}

Using a HashSet is very efficient as it will use the int hash of the string to find the bucket, only then needing to use equals to do a full ‘expensive’ equals.

1

solved Searching Duplicate String Complexity