logo CodeStepByStep logo

removeDuplicates

Language/Type: C++ array lists arrays

Write a member function named removeDuplicates that could be added to the ArrayIntList class. Your function should remove any duplicate elements from the list, such that after a call to your function, the list will contain a set of unique elements. To simplify the problem, we will assume that the list is sorted, so that all duplicates occur consecutively. Suppose an ArrayIntList variable named list stores the following values:

{7, 7, 18, 18, 18, 18, 21, 39, 39, 42, 42, 42}

After a call of list.removeDuplicates(); , the list should store the following values:

{7, 18, 21, 39, 42}

If the list is empty or does not contain any duplicate values, calling your function should have no effect.

Constraints: You may call other private member functions of the ArrayIntList if you like, but not public ones. Do not make assumptions about how many elements are in the list or about the list's capacity. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Write the member function as it would appear in ArrayIntList.cpp. You do not need to declare the function header that would appear in ArrayIntList.h. Assume that you are adding this method to the ArrayIntList class as defined below:

class ArrayIntList {
private:
    int* elements;   // array storing element data
    int mysize;      // number of elements in the array
    int capacity;    // array's length

public:
    ...
};
Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.