In this article, you will learn Java program to remove duplicate elements in an Array. Here, you will see total 4 approaches to do this program using Java. To understand first three approach, you should have basic understanding of integer array and looping concept. And for the last approach you should have the java collection framework knowledge.
Approach #1. Remove duplicate element in Array using temporary Array
package com.javacodepoint.array;
public class RemoveDuplicateInArrayDemo {
// Method to remove duplicate elements
public static int removeDuplicate(int arr[]) {
int n = arr.length;
// There is no duplicate element in array if length is 0 or 1
if (n == 0 || n == 1) {
return n;
}
// Temporary array to hold unique elements
int[] temp = new int[n];
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[n - 1];
// Update the original array
for (int i = 0; i < j; i++) {
arr[i] = temp[i];
}
return j;
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer array
int arr[] = { 1, 1, 4, 5, 5, 8, 10, 10, 20, 20 };
int length = removeDuplicate(arr);
// After removing the duplicate elements, printing unique elements
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
1 4 5 8 10 20
Approach #2. Remove duplicate element in Array using separate index
package com.javacodepoint.array;
public class RemoveDuplicateInArrayDemo2 {
// Method to remove duplicate elements
public static int removeDuplicate(int arr[]) {
// array length
int n = arr.length;
// There is no duplicate element in array if length is 0 or 1
if (n == 0 || n == 1) {
return n;
}
// Separate index
int index = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1]) {
arr[index++] = arr[i];
}
}
arr[index++] = arr[n - 1];
return index;
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer array
int arr[] = { 5, 5, 10, 15, 16, 16, 20, 20, 25 };
int length = removeDuplicate(arr);
// After removing the duplicate elements, printing unique elements
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
5 10 15 16 20 25
NOTE: In the first two approachs, the array should be in sorted order otherwise it will not work.
Approach #3. Remove duplicate elements in unsorted Array
package com.javacodepoint.array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class RemoveDuplicateInArrayDemo3 {
// Method to remove duplicate elements
public static int removeDuplicate(int arr[]) {
// array length
int n = arr.length;
// There is no duplicate element in array if length is 0 or 1
if (n == 0 || n == 1) {
return n;
}
// Separate index
int index = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1]) {
arr[index++] = arr[i];
}
}
arr[index++] = arr[n - 1];
return index;
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer array
int arr[] = { 15, 5, 0, 15, 5, 16, 20, 20, 10 };
// Sort the array
Arrays.sort(arr);
int length = removeDuplicate(arr);
// After removing the duplicate elements, printing unique elements
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
0 5 10 15 16 20
Approach #4. Remove duplicate elements in Array using Collection framework
In this approach, we use the java.util.HashSet class from java collection framework to write the below program. The HashSet class doesn’t allow you to add duplicate element into it.
package com.javacodepoint.array;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class RemoveDuplicateInArrayDemo4 {
// Method to remove duplicate elements
public static int removeDuplicate(int arr[]) {
// array length
int n = arr.length;
// There is no duplicate element in array if length is 0 or 1
if (n == 0 || n == 1) {
return n;
}
// To hold only unique elements
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
set.add(arr[i]);
}
// Update the original array
Iterator<Integer> it = set.iterator();
int index = 0;
while (it.hasNext()) {
arr[index++] = it.next();
}
return index;
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer array
int arr[] = { 5, 5, 10, 15, 16, 16, 20, 20, 25 };
int length = removeDuplicate(arr);
// After removing the duplicate elements, printing unique elements
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
16 20 5 25 10 15
See also: Java Program to Calculate Average Using Array.