-2

I am trying to delete any duplicates but not having much success..

void deleatingRepeatingElement (int myArrayLength, int myArray[])
    {
        for (int i = 1 ; i < myArrayLength; i++){
        // start at second index because you don't need to compare the first element to anything, it can't have duplicate that comes first
            for (int j = 0; j < i ; j++){
                if (myArray[i] == myArray[j]){
                    myArray[j] = myArray[j + 1];
                    myArrayLength--;
            }
        }
      }
    }
7
  • 1
    when you are trying to remove an item inside an array at position j, you need to move everything after it 1 position before. Commented Nov 28, 2017 at 0:38
  • 1
    Remember that passing myArrayLength by value won't change it in the caller. Commented Nov 28, 2017 at 0:38
  • 1
    std::unique Commented Nov 28, 2017 at 0:38
  • if you had a linked list, you could just remove a node and reconnect the bits, if you have a real contiguous array you have to ripple/copy from myArray[duplicateIndex+1] to myArray[duplicateIndex] and add one to the index until you run out of array. Commented Nov 28, 2017 at 0:40
  • Another alternative to std::unique will be inserting each of the item inside the array into std::set and let it handle duplication for you Commented Nov 28, 2017 at 0:43

2 Answers 2

0

I think there were two main mistakes:

  • You didn't shift all of the following items when deleting.
  • You didn't "reset" after deleting.

Here is annotated code that seems to work:

#include <iostream>

/* Remove element at given index from array
* Returns the new array length
* (Note that "int array[]" means exactly the same as "int *array",
* so some people would consider "int *array" better style)
*/
int arrayRemoveAt(int index, int array[], int arrayLength)
{
    // Check whether index is in range
    if (index < 0 || index >= arrayLength)
        return arrayLength;

    for (int i = index + 1; i < arrayLength; i++)
    {
        array[i - 1] = array[i];
    }

    return arrayLength - 1;
}

/*
* Returns the new length of the array
*/
int deleatingRepeatingElement(int myArrayLength, int myArray[])
{
    for (int i = 1; i < myArrayLength; i++)
    {
        // start at second index because you don't need to compare the first element to anything, it can't have duplicate that comes first
        for (int j = 0; j < i; j++)
        {
            if (myArray[i] == myArray[j])
            {
                myArrayLength = arrayRemoveAt(i, myArray, myArrayLength);
                // After deleting an entry, we must "reset", because now the index i
                // might point to another number, which may be a duplicate
                // of a number even before the current j.
                // The i-- is so that after i++, we will end up with the same i
                i--;
                break;
            }
        }
    }

    // Important: The caller needs this for looping over the array
    return myArrayLength;
}

int main(int argc, char **argv)
{
    int array[] = {5, 6, 2, 1, 2, 6, 6};

    int newSize = deleatingRepeatingElement(7, array);

    for (int i = 0; i < newSize; i++)
    {
        std::cout << array[i] << std::endl;
    }

    return 0;
}

If you use a static array (such as in my example, as opposed to a dynamic one), you may consider using std::array or a template construction as shown in https://stackoverflow.com/a/31346972/5420386.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution to your problem:

#include <iostream>
#include <set>
#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0])))
using namespace std;

int *deleteRepeatedElements(int myArray[], int arrayLength) {
  set<int> setArray (myArray, myArray+arrayLength);

  int setLength = setArray.size();
  static int myPointer[4];
  int i = 0;
  for (set<int>::iterator it = setArray.begin(); it != setArray.end(); ++it) {
    myPointer[i] = *it;
    i++;
  }

  return myPointer;
}

int main() {

  int myArray[6] = {5, 3, 5, 6, 2, 4};
  int arrayLength = ARRAY_SIZE(myArray);
  int* myPointer = deleteRepeatedElements(myArray, arrayLength);

  int pointerLength = sizeof(myPointer)/sizeof(*myPointer);
  for (int* i = &myPointer[0]; *myPointer != 0; i = ++myPointer) {
    cout << *i << " ";
  }
  cout << '\n';

  return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.