Skip to content

Commit

Permalink
Remove duplicates from sorted array
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 1, 2017
1 parent 6d998e7 commit c78db79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 026_remove_duplicates_from_sorted_array/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test rm_dup.c
36 changes: 36 additions & 0 deletions 026_remove_duplicates_from_sorted_array/rm_dup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>

static int removeDuplicates(int* nums, int numsSize)
{
if (numsSize <= 1) {
return numsSize;
}

int i = 0, j, count = 1;
while (i < numsSize) {
for (j = i + 1; j < numsSize && nums[i] == nums[j]; j++) {}
if (j < numsSize) {
nums[count++] = nums[j];
}
i = j;
}

return count;
}

int main(int argc, char **argv)
{
int i, size = argc - 1;
int *nums = malloc(size * sizeof(int));
for (i = 0; i < argc - 1; i++) {
nums[i] = atoi(argv[i + 1]);
}

int count = removeDuplicates(nums, size);
for (i = 0; i < count; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}

0 comments on commit c78db79

Please sign in to comment.