Remove duplicate elements from array in Java
In this post, you will learn how to remove duplicate elements from an array in Java programming.
An array is a collection of key/value pairs stored at contiguous memory locations. We can store more than one item in only one variable, i.e., Array. So, an array is used when there is a requirement to add more items in a single variable.
Sometimes in the development process, we may face a situation where we need to filter arrays to remove duplicates. There are lots of ways to remove the duplicate values from an array in Java. Here, we have mentioned most of them -
Remove duplicate elements from array using Temporary Array
In the given example, we have created a temporary array to store the unique array elements. The initial array is traversed, and the unique elements are copied to the temporary array. We declared a variable "j" to keep track of the count of the unique element. Furthermore, the value held in "j" is then copied from the temporary array to the initial array, after which "j" is returned.
public class RemoveDuplicateElements{
// declaring function to remove duplicate elements
public static int removeDuplicateElements(int arr[], int num){
if (num==0 || num==1){
return num;
}
// initializing temporary array
int[] temp = new int[num];
int j = 0;
for (int i=0; i<num-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[num-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {30, 23, 42, 12, 23, 42, 52};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
Output of the above code:
30 23 42 12 23 42 52
Remove duplicate elements from array using Separate Index
In the given example, we have used the separate index process to remove the duplicate elements from an array.
// Java program to remove
// duplicate elements
public class RemoveDuplicateElements{
public static int removeDuplicates(int array[], int len){
if(len==0 || len==1){
return len;
}
int j = 0;
for (int i=0; i < len-1; i++){
if (array[i] != array[i+1]){
array[j++] = array[i];
}
}
array[j++] = array[len-1];
return j;
}
public static void main (String[] args) {
int array[] = {22, 22, 43, 43, 53, 62};
// getting array length
int length = array.length;
// calling function
length = removeDuplicates(array, length);
//printing array elements
for(int i=0; i<length; i++)
System.out.print(array[i]+" ");
}
}
Output of the above code:
22 43 53 62
Remove duplicate elements from array using LinkedHashSet
In the given example, we have used the LinkedHashSet to remove duplicate elements from an array. Using this, we have added all elements of the given array into the LinkedHashSet, and then we have converted the linkedhashSet to the array. The result array does not contain any duplicate elements.
import java.util.Arrays;
import java.util.LinkedHashSet;
public class RemoveDuplicateElements
{
public static void main(String[] args) throws CloneNotSupportedException
{
// initializing array
Integer[] arr = new Integer[] {22, 42, 22, 63, 42, 73, 63};
// create set from array elements
LinkedHashSet linkedHashSet = new LinkedHashSet<>( Arrays.asList(arr) );
// getting array back after removing duplicates
Integer[] result = linkedHashSet.toArray(new Integer[] {});
//printing array elements
System.out.println( Arrays.toString(result) );
}
}
Output of the above code:
[22, 42, 63, 73]
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number