This is the Java Program to Remove Duplicates in a Sorted Array.
Given a sorted array of integers, remove the duplicates of the elements from the array.
Example:
Array = [1 2 2 3 3 4]
Output
Array = [1 2 3 4]
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.
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.
//Java Program to Remove Duplicates in a Sorted Array
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RemoveDuplicates {
// Function to remove the duplicates
static int removeDuplicates(int[] array){
int replaceIndex = 0;
int i,j;
for(i=0; i<array.length; i++){
for(j=i+1; j<array.length; j++){
if(array[j]!=array[i]){
break;
}
}
array[replaceIndex++] = array[i];
i = j-1;
}
return replaceIndex;
}
// Function to read user input
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements in sorted order");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
}
int index = removeDuplicates(array);
System.out.println("Array after removing duplicates is");
for(i=0; i<index; i++){
System.out.print(array[i] + " ");
}
}
}
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.
Time Complexity: O(n) where n is the number of elements in the array.
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.
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Java Books
- Practice BCA MCQs
- Practice Information Technology MCQs