C Program to Remove Duplicates from Sorted Array Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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. C #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; } Output1 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 ContentBy Frequency CountingUsing Another ArrayBy Frequency CountingThis method uses a temporary frequency array to count the occurrences of each element and then constructs the array with unique elements. C++ #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; } Output1 2 3 4 5 Using Another ArrayIf 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. C #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; } Output1 2 3 4 5 Comment More infoAdvertise with us Next Article C Program to Remove Duplicates from Sorted Array kartik Follow Improve Article Tags : C Programs C Language c-array C Array Programs C Examples +1 More Similar Reads C Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43- 5 min read C# Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43- 8 min read C Program to Remove All Occurrences of an Element in an Array To remove all the occurrences of an element in an array, we will use the following 2 approaches: Using MethodsWithout using methods or With Conditional Statement We will keep the same input in all the mentioned approaches and get an output accordingly. Input: array = {1, 2, 1, 3, 1} value = 1 Output 3 min read C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read C Program to Sort the Elements of an Array in Descending Order Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending 3 min read C Program to Find Common Array Elements Here, we will see how to find the common array elements using a C program. Input: a[6] = {1,2,3,4,5,6} b[6] = {5,6,7,8,9,10} Output: common array elements is 5 6 C // C program to find the common array elements #include <stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int b[6] = { 5, 6, 1 min read C Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg 3 min read C Program For Bubble Sort Bubble Sort is a comparison based simple sorting algorithm that works by comparing the adjacent elements and swapping them if the elements are not in the correct order. It is an in-place and stable sorting algorithm that can sort items in data structures such as arrays and linked lists.Implementatio 4 min read How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read C Program For Insertion Sort Insertion sort is a simple sorting algorithm used to sort a collection of elements in a given order. It is less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort but it is simple to implement and is suitable to sort small data lists.In this article, we 4 min read Like