Remove First Name Duplicates

#1

I think I found a bug in this problem.

 public static void eliminateDuplicate(List<Name> A) {
    Collections.sort(A); // Makes identical elements become neighbors.
    int writeIdx = 1;
    for (int i = 1; i < A.size(); i++) {
        if (!A.get(i).firstName.equals(A.get(writeIdx).firstName)) {
            A.set(++writeIdx, A.get(i));
        }
    }
    // Shrinks array size.
    A.subList(++writeIdx, A.size()).clear();
}

writeIdx is initialized to 1 and i is initialized to 1 there for if I give an input of 2 names with just David as the first name there will be a duplicate. How can this be fixed?

0 Likes

#2

This is a bug fixed recently, and the fix is simple, changing the write_idx initialized to 0.

1 Like