This is the second solution to remove the duplicate numbers from an Array of integers. This program uses the Set collection to removing the duplicate numbers.
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateNumbersRemovalUsingHashSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer [] arrNums= {23,44,13,754,128,23,200,44};
//Convert the given Integer array to the Arraylist of Integers
List<Integer> arrList=Arrays.asList(arrNums);
System.out.println("The original Array given below");
System.out.println(arrList);
//Use the Set Collection to create new Set using above Arraylist
System.out.println("The original Array after removing the duplicates");
Set<Integer> individualNums=new HashSet<Integer>(arrList);
Integer [] finalArray=new Integer[individualNums.size()];
individualNums.toArray(finalArray);
for(int i=0;i<finalArray.length;i++)
System.out.println(finalArray[i]);
}
}
The original Array given below
[23, 44, 13, 754, 128, 23, 200, 44]
The original Array after removing the duplicates
128
754
23
200
44
13

Leave a comment