Java Remove duplicates Write a method that removes the dupli
Java
(Remove duplicates) 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:Solution
This is java program it cannot repeat same element .
class duplicate {
public static int[] removeDuplicates(int[] arr) {
Set<Integer> alreadyPresent = new HashSet<Integer>();
int[] whitelist = new int[0];
for (int nextElem : arr) {
if (!alreadyPresent.contains(nextElem)) {
whitelist = Arrays.copyOf(whitelist, whitelist.length + 1);
whitelist[whitelist.length - 1] = nextElem;
alreadyPresent.add(nextElem);
}
}
return whitelist;
}}
