fork download
  1. #include<stdio.h>
  2.  
  3. unsigned remove_dumped_memory(unsigned *array, unsigned nItem);
  4.  
  5. int main(){
  6. int i;
  7. unsigned size = 10;
  8. unsigned a[10] = {1,1,2,2,2,3,5,8,8,9};
  9.  
  10. size = remove_dumped_memory(a, size);
  11.  
  12. printf("content of array:\n");
  13. for(i = 0; i < size; i++){
  14. printf("%d ", a[i]);
  15. }
  16. printf("\narray size: %d\n", size);
  17.  
  18. return 0;
  19. }
  20.  
  21. unsigned remove_dumped_memory(unsigned *array, unsigned nItem){
  22. int cnt=1;
  23. int left = 1, right = nItem-1, mid = (left+right)/2;
  24. unsigned key = array[0];
  25.  
  26. while(left <= right){
  27. if(key != array[left]){
  28. array[cnt] = array[left];
  29. key = array[left];
  30. left++;
  31. mid = (left+right)/2;
  32. cnt++;
  33. }else{
  34. if(key == array[mid]){
  35. left = mid+1;
  36. }else{
  37. mid = (left+mid)/2;
  38. }
  39. }
  40. }
  41. return cnt;
  42. }
  43.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
content of array:
1 2 3 5 8 9 
array size: 6