📊📚Intermediate
Remove Duplicate Elements From Sorted Array
Remove duplicates
Remove duplicate elements from a sorted array, keeping only unique values.
Program Code
remove_duplicates.c
C
1#include <stdio.h>23int main() {4 int arr[] = {1, 1, 2, 2, 3, 4, 4, 4, 5};5 int n = sizeof(arr) / sizeof(arr[0]);6 7 printf("Original: ");8 for (int i = 0; i < n; i++) printf("%d ", arr[i]);9 10 // Remove duplicates from sorted array11 int j = 0;12 for (int i = 1; i < n; i++) {13 if (arr[i] != arr[j]) {14 j++;15 arr[j] = arr[i];16 }17 }18 int newSize = j + 1;19 20 printf("\nAfter removing duplicates: ");21 for (int i = 0; i < newSize; i++) {22 printf("%d ", arr[i]);23 }24 printf("\n");25 26 return 0;27}Output
Original: 1 1 2 2 3 4 4 4 5
After removing duplicates: 1 2 3 4 5
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials