removeDuplicates: For every sequence of consecutive identical items in a
, retain only one item of that sequence.
--
int removeDups(string a[], int n);
For every sequence of consecutive identical items in a
, retain only one item of that sequence. Suppose we call the number of all retained items r. Then when this functions returns, elements 0 through r-1 of a
must contain the retained items (in the same relative order they were in originally), and the remaining elements may have whatever values you want. Return the number of retained items. Here's an example:
string d[9] = {
"charlie", "november", "alpha", "alpha", "kilo", "kilo", "ketanji", "alpha", "alpha"
};
int p = removeDups(d, 9); // returns 5
// d[0] through d[4] now contain "charlie" "november" "alpha" "kilo" "alpha"
// We no longer care what strings are in d[5] and beyond.
Psuedocode
int removeDups(string a[], int n){//return (means to leave this method), if array is empty or contains a single element because there wouldn't be any duplicatesif (n==0 || n==1)return n;//create and use a method for sorting the array in alphatical ordersortArray(a, n);string temp[n];//go through each elements in the arrayint j = 0;for (int i=0; i<n-1; i++) {//if a[i] is NOT equal to a[i+1]//store a[i] in temp[j++], temp array is used to store all the unique strings}//store a[n-1] into temp[j++] outside the for loop//modify original array by putting in the temp array back into 'a' arrayfor (int i=0; i<j; i++)a[i] = temp[i];//j is the size of the new arrayreturn j;}
Solution
void sortArray(string a[], int n) {int i, j, min;string temp;for (i = 0; i < n-1; i++) {min = i;for (j = i+1; j < n; j++){if (a[j] < a[min]){min = j;