I am trying to find the number of Strings that only appear exactly once in an `ArrayList`.

How many I achieve this (preferably with the best possible time complexity)?

Below is my method:

```
  public static int countNonRepeats(WordStream words) {

    ArrayList<String> list = new ArrayList<String>();
    for (String i : words) {
      list.add(i);
    }

    Collections.sort(list);

    for (int i = 1; i < list.size(); i++) {
      if (list.get(i).equals(list.get(i - 1))) {
        list.remove(list.get(i));
        list.remove(list.get(i - 1));
      }
    }

    System.out.println(list);

    return list.size();
  }

```

Why doesn't it remove the String at `list.get(i)` and `list.get(i-1)`?
['https://Stackoverflow.com/questions/35996096', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5938067/']
Here's one simple suggestion: 

1. First, sort your array by alphanumerical order
2. Iterate through with a loop, `if( !list.get(i).equals(list.get(i+1)) ) → unique`
3. If you find duplicates, increment `i` until you reach a different string

This will have the complexity of the sorting algorithm, since step 2+3 should be O(n)
There is no need for sorting.
A better approach would be to use two HashSet One for maintaining repeating and one for non-repeating words. Since HashSet internally uses HashMap, Ideally contains, get, put operation has o(1) complexity. So the overall complexity of this approach would be o(n). 

```
    public static int countNonRepeats(List<String> words) {

    Set<String> nonRepeating = new HashSet<String>();
    Set<String> repeating = new HashSet<String>();

    for (String i : words) {
        if(!repeating.contains(i)) {
            if(nonRepeating.contains(i)){
                repeating.add(i);
                nonRepeating.remove(i);
            }else {
                nonRepeating.add(i);
            }
        }
    }

    System.out.println(nonRepeating.size());

    return nonRepeating.size();
}

```