In this tutorial, you will learn how to write java program to remove duplicate elements in a given array. We will write two java programs in this article. First program will remove the duplicate elements from a sorted array and the second program will remove the duplicates from unsorted array.
Illustration:
Array: {1, 2, 2, 3, 3, 4} Array after removing duplicates: {1, 2, 3, 4}
Example 1: Program to remove duplicate elements from a sorted array
In this example, the given array is a sorted array. To remove the duplicates from an unsorted array, refer the second example.
public class JavaExample{ public static int removeDuplicates(int arr[], int count){ if (count==0 || count==1){ return count; } // creating a temporary array to hold non-duplicate elements int[] temp = new int[count]; int j = 0; for (int i=0; i<count-1; i++){ if (arr[i] != arr[i+1]){ temp[j++] = arr[i]; } } temp[j++] = arr[count-1]; // copying the temp array to the original array for (int i=0; i<j; i++){ arr[i] = temp[i]; } return j; } public static void main (String[] args) { int arr[] = {1, 2, 2, 3, 4, 5, 5, 5}; System.out.print("Original Array: "); int length = arr.length; for (int i=0; i<length; i++) System.out.print(arr[i]+" "); //getting the new array size after removing duplicates length = removeDuplicates(arr, length); //for new line System.out.println(""); //Displaying array with non-duplicate elements System.out.print("Array after removing duplicate elements: "); for (int i=0; i<length; i++) System.out.print(arr[i]+" "); } }
Output:
Original Array: 1 2 2 3 4 5 5 5 Array after removing duplicate elements: 1 2 3 4 5
Example 2: Program to remove duplicate elements from an unsorted array
This program is same as the above program except that here the given array is unsorted. The workaround is pretty simple, we can import java.util.Arrays
package and use the Arrays.sort()
method to sort the unsorted array and then use the same logic that we have used above to remove duplicate elements.
import java.util.Arrays; public class JavaExample{ public static int removeDuplicates(int arr[], int count){ if (count==0 || count==1){ return count; } // creating a temporary array to hold non-duplicate elements int[] temp = new int[count]; int j = 0; for (int i=0; i<count-1; i++){ if (arr[i] != arr[i+1]){ temp[j++] = arr[i]; } } temp[j++] = arr[count-1]; // copying the temp array to the original array for (int i=0; i<j; i++){ arr[i] = temp[i]; } return j; } public static void main (String[] args) { int arr[] = {3, 2, 1, 2, 9, 10, 4, 10, 9}; System.out.print("Original Array: "); //Sorting the given unsorted array Arrays.sort(arr); int length = arr.length; for (int i=0; i<length; i++) System.out.print(arr[i]+" "); //getting the new array size after removing duplicates length = removeDuplicates(arr, length); //for new line System.out.println(""); //Displaying array with non-duplicate elements System.out.print("Array after removing duplicate elements: "); for (int i=0; i<length; i++) System.out.print(arr[i]+" "); } }
Output:
Original Array: 1 2 2 3 4 9 9 10 10 Array after removing duplicate elements: 1 2 3 4 9 10
Leave a Reply