From 66f0f953eefc68947e01a3b76e69831bb86deac6 Mon Sep 17 00:00:00 2001 From: pabir Date: Sun, 2 Jul 2023 20:46:29 +0530 Subject: [PATCH] removing duplicate from a sorted array --- .../removing_duplicate_from_a_sorted_array.c | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 algorithms/C/arrays/removing_duplicate_from_a_sorted_array.c diff --git a/algorithms/C/arrays/removing_duplicate_from_a_sorted_array.c b/algorithms/C/arrays/removing_duplicate_from_a_sorted_array.c new file mode 100644 index 00000000..a6ed38d6 --- /dev/null +++ b/algorithms/C/arrays/removing_duplicate_from_a_sorted_array.c @@ -0,0 +1,36 @@ +#include + +int removeDuplicates(int arr[], int n) +{ + if (n == 0 || n == 1) + return n; + + int temp[n]; + + int j = 0; + int i; + for (i = 0; i < n - 1; i++) + if (arr[i] != arr[i + 1]) + temp[j++] = arr[i]; + + temp[j++] = arr[n - 1]; + + for (i = 0; i < j; i++) + arr[i] = temp[i]; + + return j; +} + +int main() +{ + int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}; + int n = sizeof(arr) / sizeof(arr[0]); + + n = removeDuplicates(arr, n); + + printf("Array after removing duplicates: "); + for (int i = 0; i < n; i++) + printf("%d ", arr[i]); + + return 0; +}