Write a static method called " removeDuplicates " that takes an ArrayList of Strings as a parameter. It should first sort the ArrayList using the sort function of the Containers helper class and then...


Write a static method called "removeDuplicates" that takes an ArrayList of Strings as a parameter.  It should first sort the ArrayList using the sort function of the Containers helper class and then remove any duplicates so that only one of any series of duplicates remains in the ArrayList.


For example, suppose that the parameter is an ArrayList named a containing:
     ["clam", "squid", "squid", "squid", "clam", "octopus", "octopus", "shrimp"]


Then after calling “removeDuplicates(list)” the ArrayList a should contain:
            ["clam", "octopus", "shrimp", "squid"]


Please note: quotation marks would not appear in the actual output of the ArrayList, they are only included here for clarity.  The actual ArrayList output would be:
     [clam, octopus, shrimp, squid]


You may use the following code to test your program:
  Expected output is listed to the right of the print statement.

import java.util.*;
public class RemoveDuplicates {
  public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("clam");
    list.add("squid");
    list.add("squid");
    list.add("squid");
    list.add("clam");
    list.add("octopus");
    list.add("octopus");
    list.add("shrimp");
    removeDuplicates(list);
    System.out.println(list);  // [clam, octopus, shrimp, squid]
  }


   // *** Your method code goes here ***



} // End of RemoveDuplicates class




Jun 07, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here