Interview Question

Interview(Student Candidate) Toronto, ON (Canada)

Write code to find duplicates in a sorted array and delete

  them.
Answer

Interview Answer

1 Answer

0

// Function to remove the duplicates int removeDuplicates(int a[], int array_size) { int i, j; j = 0; // Print old array... printf("\n\nOLD : "); for(i = 0; i < array_size; i++) { printf("[%d] ", a[i]); } // Remove the duplicates ... for (i = 1; i < array_size; i++) { if (a[i] != a[j]) { j++; a[j] = a[i]; // Move it to the front } } // The new array size.. array_size = (j + 1); // Print new array... printf("\n\nNEW : "); for(i = 0; i< array_size; i++) { printf("[%d] ", a[i]); } printf("\n\n"); // Return the new size... return(j + 1); }

Dominator on Sep 2, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.