Simple Java programs that remove duplicate elements from a given array.
This is a bit tricky problem. In this, we have to remove all the duplicate elements from the array. for example, {2,3,5,5,6,2}, as you can see there are some duplicates can be seen and the task is to remove these elements so that we can get an array of unique elements, that is {2,3,5,6}.
Given an array and we need to remove duplicate elements using Java program.
Input array elements: 1, 2, 3, 1, 2, 3, 4 Output: 1, 2, 3, 4
There are various ways to remove duplicate elements from array, here are few of them:
- Directly removing duplicate elements from array
- Eliminating duplicate elements by sorting the array first
- Removing duplicate elements using LinkedHashSet
- Getting unique elements using Streams ( Java 8 Feature )
1. Java program to remove duplicate elements from an array
Program:
public class Duplicate_int { public static void main(String[] args) { int index=0; int[] arr = {10,20,20,50,50,45}; int size = arr.length; int duplicate,flag=0,count; int[] anti_dup= new int[size]; int i,j; System.out.println("Before removing duplicate from the array:"); for(i=0;i<size;i++) { System.out.println(arr[i]); } for(i=0;i<size;i++) { count=0; for(j=i+1;j<size;j++) { if(arr[i]==arr[j]) { count=count+1; break; } } if(count==0) { anti_dup[index]=arr[i]; index++; } } for(i=0;i<index;i++) { arr[i]=anti_dup[i]; } System.out.println("After removing duplicate from the array"); for(i=0;i<index;i++) { System.out.println(arr[i]); } } }
Output:
Before removing duplicate from the array: 10 20 20 50 50 45 After removing duplicate from the array 10 20 50 45
2. Java program to remove duplicate elements from an array By Sorting it first
Program:
import java.util.*; public class RemoveDuplicate { public static void main(String[] args) { int[] arr = {6, 2, 3, 4, 43, 6, 7, 5, 6, 34, 4}; int[] uniqueArr = new int[arr.length]; int counter = 0; Arrays.sort(arr); for (int i = 0; i < arr.length - 1; i++) { if (arr[i] != arr[i + 1]) { uniqueArr[counter] = arr[i]; counter++; } } uniqueArr[counter] = arr[arr.length - 1]; System.out.println("Array with Unique Elements : "); for (int i = 0; i <= counter; i++) { System.out.println(uniqueArr[i]); } } }
Output:
Array with Unique Elements : 2 3 4 5 6 7 34 43
3. Remove Duplicate Elements using Streams
Program:
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class RemoveDuplicateExample { public static void main(String[] args) { Integer[] numberArr = new Integer[] {10,30,30,10,15,13,34,15}; List<Integer> uniqueList = Arrays.asList(numberArr).stream().distinct().collect(Collectors.toList()); for (Integer singleElement: uniqueList ) { System.out.println(singleElement); } } }
Output:
10 30 15 13 34
Explanation:
In above code, we first converted given array to list, then eliminated duplicated using distinct function and collected it in form of list. Lastly, we printed the list.
4. Removing duplicates using LinkedHashSet
Program:
import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; public class RemoveDuplicateExample { public static void main(String[] args) { Integer[] numberArr = new Integer[] {10,30,30,10,15,13,34,15}; Set<Integer> uniqueSet = new LinkedHashSet<>(Arrays.asList(numberArr)); for (Integer singleElement: uniqueSet ) { System.out.println(singleElement); } } }
Output:
10 30 15 13 34
Explanation:
In above program, we used linkedHashSet in order to have unique elements and to maintain the order. We all know, a set doesn’t allow duplicates, therefore, using that property we’re able to remove the duplicates.