-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--;
            }
        }
      }
    }
Gihan Chathuranga
  • 442
  • 10
  • 16
Yosefi
  • 1
  • 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. – lamandy Nov 28 '17 at 00:38
  • 1
    Remember that passing `myArrayLength` by value won't change it in the caller. – drescherjm Nov 28 '17 at 00:38
  • 1
    [std::unique](http://en.cppreference.com/w/cpp/algorithm/unique) – O'Neil Nov 28 '17 at 00: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. – Grady Player Nov 28 '17 at 00: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 – lamandy Nov 28 '17 at 00:43
  • Note that if you want to use std::unique indeed, you need to sort first! –  Nov 28 '17 at 00:45
  • ok so I changed some things and I still am not getting it to work. – Yosefi Nov 28 '17 at 11:47

2 Answers2

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.

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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Josimar Lopes
  • 156
  • 1
  • 2
  • 9