title: How to remove and replace an item in an array list in Java?
body: <p>I have the following code in java,</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Objects;
  
  public class Cars {
  
      private static ArrayList&lt;String&gt; replaceDuplicates(ArrayList&lt;String&gt; aList) {
  
          for (int i = 0; i &lt; aList.size(); i++) {
              for (int j = i + 1; j &lt; 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&lt;String&gt; cars = new ArrayList&lt;&gt;();
  
          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);
  
      }
  
  }
  </code></pre>
  
  <p>The output of this code is - <code>[, , Hyundai, , , Toyota, Ford, Honda, GMC]</code></p>
  
  <p>I want to replace the name of cars that appear more than once in the array list with a <code>" "</code>. 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 <code>" "</code>. </p>
  
  <p>My desired output should be like this - <code>[, , Hyundai, , , , , Honda, GMC]</code></p>
  
  <p>What am I doing wrong here?</p>
  
  <p>Thank you in advance!</p>