Member-only story
removeDuplicates: For every sequence of consecutive identical items in a
, retain only one item of that sequence.
2 min readMay 4, 2022
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++) {