Posted By

jaircazarin on 10/09/08


Tagged

programming Algorithms Microsoft-Interview


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jaircazarin
sergeizen
skammer


Delete Repeated Numbers


 / Published in: C
 

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
  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  

You need to login to post a comment.