-1

i have an array full of values, the array can not contain any duplicate values. for any duplicate value add one to the value. here is the code i have so far, but im still getting duplicates. (randArray is where the values are located).

for (int i = 0; i < sizeof(randArray) - 1; i++) {
    for (int j = sizeof(randArray); j == 0; j--) {
        if (randArray[i] == randArray[j]) {
            randArray[i] == randArray[i] + 1;
        }
    }
}
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
noam
  • 9
  • 2

5 Answers5

2

You have a typo when incrementing a duplicate:

     randArray[i] = randArray[i] + 1; // not ==

Also, the increment might create another duplicate. If it's with an item that comes afterwards there's no problem. But as the array is not sorted, you might not catch such a new duplicate of a value already passed.

Therefore you might need several passes:

 bool wasaninc;
 do {
      wasaninc=false;
      for ...
          for ...
              ... // if there is an increment, set wasaninc to true
  } while (wasaninc);
Christophe
  • 68,716
  • 7
  • 72
  • 138
0
Change  randArray[i] == randArray[i] + 1; to randArray[i] = randArray[i] + 1;

   for (int i = 0; i < sizeof(randArray) - 1; i++) {
        for (int j = sizeof(randArray); j == 0; j--) {
            if (randArray[i] == randArray[j]) {
                randArray[i] = randArray[i] + 1;
            }
        }
    }
Harminder
  • 141
  • 1
  • 8
0

Your problem is due to sizeof(randArray). This method doesn't return the number of elements that are in the array.

For example:

int array[5] = { 1, 2, 3, 4, 5};
sizeof(array);   // -> returns 20, because of 5 * 4 bytes (integer = 4 bytes)

Instead of using this method you should actually use the number of elements in the array. You declared the size of the array already in the beginning. So it is clear how many elements can be in the array.

Correct example:

int array[100] = {...};
for (int i = 0; i < 99; i++) {
     for (int j = 0; j < 99; j++) {
          if (array[i] == array[j]) {
              // code for duplicates
          }
      }
}
Jonas Aisch
  • 71
  • 10
0
#include <iostream>
#include <unistd.h>
#include <algorithm>

using namespace std;

int main()
{
    int arr[8]={0,1,1};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout<<n<<"\n";
  
    /*Here we take two parameters, the beginning of the
    array and the length n upto which we want the array to
    be sorted*/
    //sort(arr, arr + n);
    for (int i=0;i<=n;i++){
        cout<<arr[i]<<" ";

    }
    int result=0;
    do{
        for (int i=0;i<=n;i++){
            for (int j =0;j<i;j++){
                if(arr[j]==arr[i]){
                    srand(time(NULL)); // Seed the time
                    int finalNum = rand()%((10-1)+1); // Generate the number, assign to variable.
                    arr[i] = finalNum;
                    result = result + 1;
                    sleep(1);
                    i--;
                    
                }
            }
        }
    }
    while(result<=2);
    n = sizeof(arr) / sizeof(arr[0]);
  
    /*Here we take two parameters, the beginning of the
    array and the length n upto which we want the array to
    be sorted*/
    //sort(arr, arr + n);
    cout<<"\n";
    for (int i=0;i<=n;i++){
        cout<<arr[i]<<" ";

    }

    return 0;
}
  • 1
    please avoid using [using namespace std;](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – yaodav Mar 30 '21 at 15:26
-1

(I'm assuming that randArray is a C-style array.) sizeof(randArray) does not return the number of elements in the array, it returns the number of bytes that randArray occupies.

Example (on wandbox):

int main()
{
    int arr[] = {1, 2, 3, 4};

    // Prints 16! (on my machine)
    std::cout << sizeof(arr) << "\n";
}

The correct way of working with arrays in modern C++ is either using std::array or std::vector. They both provide a .size() method that returns the number of elements in the collection.

Your code is failing for these reasons:

  • sizeof does not return the number of elements in the array.

  • Incrementing a duplicate element by one does not guarantee that it will be unique.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416