Write a method that removes the duplicate elements from an a
Write a method that removes the duplicate elements from an array list of integers using the following header:
public static void removeDuplicate(ArrayList list)
Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run:
Enter ten integers: 10 20 30 20 20 30 50 60 100 9
The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Solution
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Duplicate {
public static void removeDuplicate(ArrayList a)
{
Set<Integer> s1 = new HashSet<>();
s1.addAll(a);
a.clear();
a.addAll(s1);
System.out.println(\"The distinct integers are : \");
for (Object a1 : a) {
System.out.print(a1 + \" \");
}
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList l1 = new ArrayList();
for(int i=0;i<10;i++)
{
System.out.println(\"Enter \" + (i+1) + \" integer values : \");
int number = input.nextInt();
l1.add(number);
}
removeDuplicate(l1);
}
}
