• + 1 comment

    Why check contains when add does the same thing, returning false when the item is already in the set? That makes it a lot simpler:

    if (!colors.add(c[i])) {
       pairs++;
       colors.remove(c[i]);
    }