Delete Repeated Numbers


/ Published in: C
Save to your folder(s)

Given a sorted array return the new length of the array with no-repeated numbers.

Interesting test cases:

* All the same number
* No repetitions
* Empty array
* Negative size
* One element


Copy this code and paste it in your HTML
  1. #include<stdio.h>
  2.  
  3. int deleteRepeatedNumbers(int list[], int size)
  4. {
  5. if(size < 1)
  6. return 0;
  7. int newLength = 1;
  8. for(int i = 1; i < size; i++)
  9. if(list[i-1] != list[i])
  10. list[newLength++] = list[i];
  11. return newLength;
  12. }
  13.  
  14. int main()
  15. {
  16. int sortedArray[] = {1, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6 ,6 ,6, 11};
  17. printf("%d", deleteRepeatedNumbers(sortedArray, 15));
  18. return 0;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.