C Program to Remove Duplicates from Sorted Array
In this article, we will learn how to remove duplicates from a sorted array using the C program.
The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are adjacent, so just check the adjacent elements replace them with first non-duplicate.
#include <stdio.h>
int removeDup(int arr[], int n) {
if (n == 0) return 0;
int j = 0;
for (int i = 1; i < n - 1; i++) {
// If a unique element is found, place
// it at arr[j + 1]
if (arr[i] != arr[j])
arr[++j] = arr[i];
}
// Return the new ending of arr that only
// contains unique elements
return j + 1;
}
int main() {
int arr[] = {1, 1, 2, 2, 3, 4, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Remove duplicates
n = removeDup(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output
1 2 3 4
Explanation: In this program, the new end is returned because we cannot actually delete an element from the static array in C. So, a pointer to the end is created to represent the logical end.
There are also a few other methods in C to remove duplicates from a sorted array. Some of them are as follows:
Table of Content
By Frequency Counting
This method uses a temporary frequency array to count the occurrences of each element and then constructs the array with unique elements.
#include <stdio.h>
#include <stdlib.h>
int max(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (max < arr[i]) max = arr[i];
}
return max;
}
// Function to remove duplicates using frequency counting
int removeDupUsingFreq(int arr[], int n) {
if (n == 0) return 0;
// Creating frequency array
int m = max(arr,n) + 1;
int* f = (int*)malloc(sizeof(int) * m);
int j = 0;
for (int i = 0; i < n; i++) {
// If element is encountered for the first time
// place it at arr[j] and mark element as seen
if (f[arr[i]] == 0) {
arr[j++] = arr[i];
f[arr[i]] = 1;
}
}
// Return the new size of the array
return j;
}
int main() {
int arr[] = {1, 1, 2, 3, 3, 4, 5, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Remove duplicates
n = removeDupUsingFreq(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
1 2 3 4 5
Using Another Array
If the original array must not be modified, instead of pointing j to the current array, point j to the new array and insert unique element to this new array using either the two-pointer approach or frequency counting.
#include <stdio.h>
int removeDup(int arr[], int n, int u[]) {
if (n == 0) return 0;
int j = 0;
for (int i = 0; i < n - 1; i++) {
// If the current element is different from
// next element, store it in u[j]
if (arr[i] != arr[i + 1])
u[j++] = arr[i];
}
// Add the last element as it is unique
// by default
u[j++] = arr[n - 1];
return j;
}
int main() {
int arr[] = {1, 1, 2, 2, 3, 4, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int u[n];
// Store unique elements in the u array
// and return new size
int m = removeDup(arr, n, u);
for (int i = 0; i < m; i++) {
printf("%d ", u[i]);
}
return 0;
}
Output
1 2 3 4 5