1

I have the following code in java,

import java.util.ArrayList;
import java.util.Objects;

public class Cars {

    private static ArrayList<String> replaceDuplicates(ArrayList<String> aList) {

        for (int i = 0; i < aList.size(); i++) {
            for (int j = i + 1; j < aList.size(); j++) {
                if (Objects.equals(aList.get(i), aList.get(j))) {
                    aList.remove(i);
                    aList.add(i, "");
                    aList.remove(j);
                    aList.add(j, "");
                }
            }
        }

        return aList;
    }

    public static void main(String[] args) {

        ArrayList<String> cars = new ArrayList<>();

        cars.add("Ford");
        cars.add("Ford");
        cars.add("Hyundai");
        cars.add("Toyota");
        cars.add("Toyota");
        cars.add("Toyota");
        cars.add("Ford");
        cars.add("Honda");
        cars.add("GMC");

        System.out.println(cars);

        cars = replaceDuplicates(cars);

        System.out.println(cars);

    }

}

The output of this code is - [, , Hyundai, , , Toyota, Ford, Honda, GMC]

I want to replace the name of cars that appear more than once in the array list with a " ". For some reason, in my code if a car's name has appeared thrice in the array list, then the third occurrence isn't getting replaced by " ".

My desired output should be like this - [, , Hyundai, , , , , Honda, GMC]

What am I doing wrong here?

Thank you in advance!

3
  • 2
    Whenever you find a pairwise match, you clear both matched names. What, then, are you comparing against subsequent names? Commented Dec 5, 2016 at 22:53
  • 2
    you can use a Set instance for this purpose Commented Dec 5, 2016 at 22:53
  • What is your desired output? I'm guessing something like [, , Hyundai, , , , , Honda, GMC], but not sure from your description. Commented Dec 5, 2016 at 23:00

2 Answers 2

1

First off: you can simplify this code by using List.set instead of inserting and removing elements.

aList.remove(i);
aList.add(i, "");

would simply become

aList.set(i, "");

You're deleting both entries, if they're duplicate. This leads to the behavior of always deleting an even number of entries. For example:

a  b  a  a  c  a  d  a
b  a  c  a  d  a         #first pair removed
b  c  d  a               #second pair removed

If the number of elements is odd, there will always remain one element in the list.

Obviously you need some way to remember what elements to delete. A simple approach to this would be to use a flag to remember whether a duplicate of an element has been encountered:

for (int i = 0; i < aList.size(); i++) {
    //the flag
    boolean duplicate = false;        

    for (int j = i + 1; j < aList.size(); j++) {
        if (Objects.equals(aList.get(i), aList.get(j))) {
            aList.set(j, "");   //remove all duplicates with an index higher than i
            duplicate = true;   //remember that element at index i is a duplicate
        }
    }

    //remove last duplicate element
    if(duplicate)
        aList.set(i, "");
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you still want to use your approach, you can create references to your items before modifying list:

for (int i = 0; i < aList.size(); i++) {
    String a = aList.get(i);
    for (int j = i + 1; j < aList.size(); j++) {
        String b = aList.get(j);
        if (Objects.equals(a, b)) {
            aList.remove(i);
            aList.add(i, "");
            aList.remove(j);
            aList.add(j, "");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.