Write the following array function in C The function must no
Write the following array function in C++:
The function must not use any function templates from the algorithms portion of the Standard C++ library.
int eliminateDups(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 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:
The function must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning.
Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.
Solution
Hi, Please find my method implementation.
Please let me know in case of any issue:
int eliminateDups(string a[], int n){
if(n < 0){
return -1;
}
int count = 0; // index to store the number of distinct element
int i = 0;
while(i < n){
// copying current element at index \'count\'
a[count] = a[i];
count++;
// skipping all consusitive elements those are equal to \'a[i]\'
while((i+1 < n) && (a[i].compare(a[i+1]) == 0))
i++;
i++; // point to next distinct element
}
return count;
}
