In few previous article, we have explained about how to write hello world program in java and various java sample programs with output, but in this post, I will be providing various java programming examples to remove duplicate elements from an array in Java.
Remove duplicates from an array in Java (Array not sorted)
In this method, first we will check duplicate array count, then we will loop through all array elements get same array position number which will be saved in different array.
Loop again main array, save result in compact array, if duplicate array position is encountered leave it otherwise save it in resulted array.
package removeDuplicates;
public class removeDuplicateArray {
public static int[] removeDuplicate(int[] input){
boolean[] isSame = new boolean[input.length];
int sameNums = 0;
//loop through array and compare values using for loop
//get same element position
for( int i = 0; i < input.length; i++ ){
for( int j = i+1; j < input.length; j++){
if( input[j] == input[i] ){ //compare same
isSame[j] = true;
sameNums++;
}
}
}
//compact the array into the result and remove same numbers from array
int[] result = new int[input.length-sameNums];
int count = 0;
for( int i = 0; i < input.length; i++ ){
if( isSame[i] == true) {
continue;
}
else{
result[count] = input[i];
count++;
}
}
return result;
}
//print array
public static void printArray(int[] input){
System.out.println("Array after removing duplicates: ");
for( int i = 0; i < input.length; i++ ){
System.out.print(input[i] + " ");
}
}
public static void main(String[] args){
//array to remove duplicates
// 77,5 are duplicates here
int[] input = {5,77,7,8,9,12,77,5,22};
//static class with static functions are called
removeDuplicateArray.printArray(removeDuplicateArray.removeDuplicate(input));
}
}
Output:
Remove Duplicates from Array using Hashset
In this method, we will be using HashSet, which is easiest way to sort to remove duplicates from an array.
package removeDuplicates;
import java.util.HashSet;
import java.util.Iterator;
public class removeDuplicateArray {
public static int[] removeDuplicate(int[] arr){
HashSet<Integer> set = new HashSet<>();
final int len = arr.length;
//changed end to length
for(int i = 0; i < len; i++){
set.add(arr[i]);
}
int[] whitelist = new int[set.size()];
int i = 0;
for (Iterator<Integer> it = set.iterator(); it.hasNext();) {
whitelist[i++] = it.next();
}
return whitelist;
}
//print array
public static void printArray(int[] input){
System.out.println("Array after removing duplicates: ");
for( int i = 0; i < input.length; i++ ){
System.out.print(input[i] + " ");
}
}
public static void main(String[] args){
//array to remove duplicates
// 77,5 are duplicates here
int[] input = {5,77,7,8,9,12,77,5,22};
//static class with static functions are called
removeDuplicateArray.printArray(removeDuplicateArray.removeDuplicate(input));
}
}
Output:
Array after removing duplicates:
5 22 7 8 9 12 77
Sort and then remove duplicates from array
In this method, we will first sort the array using Arrays.sort()
Method, once the array is sorted, we will loop through each element array and check if adjacent element is same, if yes, leave the element and move to next element.
If main Array element is not same, save it in new Array.
package removeDuplicates;
import java.util.Arrays;
public class removeDuplicateArray {
public static int[] removeDuplicate(int[] arr){
//sort array using Arrays.Sort
Arrays.sort(arr);
int j = 0;
//loop through sorted array
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] != arr[i + 1]) {
arr[j] = arr[i];
j++;
}
}
//new array to save sorted array
int[] arrNew = new int[j+1];
arr[j] = arr[arr.length - 1];
//loop through array and save it in new array
for (int i = 0; i <= j; i++) {
arrNew[i]=arr[i];
}
return arrNew;
}
//print array
public static void printArray(int[] input){
System.out.println("Array after removing duplicates: ");
for( int i = 0; i < input.length; i++ ){
System.out.print(input[i] + " ");
}
}
public static void main(String[] args){
//array to remove duplicates
// 77,5 are duplicates here
int[] input = {5,77,7,8,9,12,77,5,22};
//static class with static functions are called
removeDuplicateArray.printArray(removeDuplicateArray.removeDuplicate(input));
}
}
Output:
Array after removing duplicates:
5 7 8 9 12 22 77
That's it.
You may also like to read:
Difference between HashMap and HashTable