In: Computer Science
Create a class named RemoveDuplicates and write code:
a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list)
b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0)
c. to output the original array that displays all integers entered
d. to output the new array that displays the list with duplicates removed
Use this TEST INPUT: 3 6 1 1 1 8 4 4 0
/* Here is your code,to run on the website whose screenshot is provided you have to rename class to Main*/
import java.util.*;
public class RemoveDuplicates {
public static <T> ArrayList<T>
removeDuplicates(ArrayList<T> list)
{
ArrayList<T> newList = new ArrayList<T>();
for (T element : list) {
if (!newList.contains(element)) {
newList.add(element);
}
}
return newList;
}
public static void display(ArrayList list){
System.out.println(list);
}
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
ArrayList<Integer>
list = new
ArrayList<>();
System.out.print("Enter list: ");
while(true){
int temp=sc.nextInt();
if(temp!=0)
list.add(temp);
else
break;
}System.out.println("original list");
display(list);
ArrayList<Integer>
newList =
removeDuplicates(list);
System.out.println("new list");
display(newList);
}
}