Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the file above using JAVA. Write a method that removes adjacent duplica

ID: 3574872 • Letter: C

Question

Complete the file above using JAVA.

Write a method that removes adjacent duplicates from an array list. Complete this code. Use the following file: Tester.java import java.util.Arrays; import java.util.ArrayList; public class Tester {public static void main(String[] args) {ArrayList words1 = new ArrayList(Arrays.asList("Typing a a word twice is is a common mistake", split(" "))); ArrayListUtil.removeAdjacentDuplicates(words1); System.out.println(words 1); Systern.out.println("Expected: [Typing, a, word, twice, is, a, common, ArrayList words2 = new ArrayList(Arrays.asList("Typing a word three times is is is not so common".split(" "))); ArrayListUtil.removeAdjacentDuplicates(words2); System.out.println(words2); System.out.println("Expected: [Typing, a, word, three, times, is, not, so, common] mistake]"); so, common]");");}} Complete the following file: ArrayListUtil .java import java.util.ArrayList; public class ArrayListUtil {/** Removes adjacent duplicates from an array list of strings. @param word an array list of strings */public static void removeAdjacentDuplicates(ArrayList words) {...}}}

Explanation / Answer

import java.util.ArrayList;

public class ArrayListUtil
{

public static void removeAdjacentDuplicates(ArrayList<String> arr){
       for(int i=arr.size()-1; i>0; i--){
           if(arr.get(i).equals(arr.get(i-1)))
               arr.remove(i);
       }
}
}

   import java.util.Arrays;
   import java.util.ArrayList;

   public class Adventure
   {
       public static void main(String[] args)
       {

       ArrayList<String> words1 = new ArrayList<String>(Arrays.asList(
       "Typing a a aword twice is is a common mistake".split(" ")));
      
       ArrayListUtil.removeAdjacentDuplicates(words1);
       System.out.println(words1);
       System.out.println("Expected:[Typing, a, word, twice, times, is, not, so, common]");
      
      
      
       ArrayList<String> words2 = new ArrayList<String>(Arrays.asList(
       "Typing a a aword three is is a common mistake".split(" ")));
      
       ArrayListUtil.removeAdjacentDuplicates(words2);
       System.out.println(words2);
       System.out.println("Expected:[Typing, a, word, three, times, is, not, so, common]");
      
   }
   }