Java Program to Remove Duplicate Elements from Array

«
»

This is the Java Program to Remove Duplicates in a Sorted Array.

Problem Description

Given a sorted array of integers, remove the duplicates of the elements from the array.
Example:

advertisement

Array = [1 2 2 3 3 4]

Output
Array = [1 2 3 4]

Problem Solution

Traverse the array from beginning to end and in a variable, an index, where the element having a duplicate will be copied. The idea is to copy each unique element to the beginning of the array and print that part of the array.

Program/Source Code
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here is the source code of the Java Program to Remove Duplicates in a Sorted Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Remove Duplicates in a Sorted Array
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class RemoveDuplicates {
  8.     // Function to remove the duplicates
  9.     static int removeDuplicates(int[] array){
  10.         int replaceIndex = 0;
  11.         int i,j;
  12.         for(i=0; i<array.length; i++){
  13.             for(j=i+1; j<array.length; j++){
  14.                 if(array[j]!=array[i]){
  15.                     break;
  16.                 }
  17.             }
  18.             array[replaceIndex++] = array[i];
  19.             i = j-1;
  20.         }
  21.         return replaceIndex;
  22.     }
  23.     // Function to read user input
  24.     public static void main(String[] args) {
  25.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  26.         int size;
  27.         System.out.println("Enter the size of the array");
  28.         try {
  29.             size = Integer.parseInt(br.readLine());
  30.         } catch (Exception e) {
  31.             System.out.println("Invalid Input");
  32.             return;
  33.         }
  34.         int[] array = new int[size];
  35.         System.out.println("Enter array elements in sorted order");
  36.         int i;
  37.         for (i = 0; i < array.length; i++) {
  38.             try {
  39.                 array[i] = Integer.parseInt(br.readLine());
  40.             } catch (Exception e) {
  41.                 System.out.println("An error occurred");
  42.                 return;
  43.             }
  44.         }
  45.         int index = removeDuplicates(array);
  46.         System.out.println("Array after removing duplicates is");
  47.         for(i=0; i<index; i++){
  48.             System.out.print(array[i] + " ");
  49.         }
  50.     }
  51. }
Program Explanation

1. In function removeDuplicates(), we declare and initialize variable replaceIndex to 0.
2. The loop for(i=0; i<array.length; i++) is used to iterate through the array.
3. The nested loop for(j=i+1; j<array.length; j++) is used to check for duplicate elements, it breaks out of the loop if the condition if(array[j]!=array[i]) is true.
4. Finally, array[i] is copied to position replaceindex and i is set to j-1, to avoid scanning again through duplicate elements.

advertisement

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
Case 1 (Positive Test Case - Involving Duplicates):
 
Enter the size of the array
6
Enter array elements in sorted order
1
2
2
3
3
4
Array after removing duplicates is
1 2 3 4
 
Case 2 (Negative test Case - Without Duplicates):
 
Enter the size of the array
4
Enter array elements in sorted order
4
3
2
1
Array after removing duplicates is
4 3 2 1
 
Case 3 (Positive Test Case - Another Example):
 
Enter the size of the array
7
Enter array elements in sorted order
1
3
3
3
4
5
6
Array after removing duplicates is
1 3 4 5 6

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.