# Thread: Question about duplicate in array algorithm (that seems to work sometimes, maybe not)

1. ## Question about duplicate in array algorithm (that seems to work sometimes, maybe not)

Hi! I'm having a problem with this algorithm that is supposed to remove duplicates from the array. The problem is that when chaging the number after the modulus ( % ) sign, the console just stands and blinks and when I press enter it does not close.

While some numbers seem to be fine, but maybe isn't? I think it is sorting the array if I have implemented the algorithm correctly, but I think there still are problems with my code, right?

So why does the console just stop? And seem to wait for something (it maybe isn't waiting for something but it looks like that for me).

Some of the comments are for the original values that were set in the program.

Edit:

I noticed that changing the constant named numberOfValues to the same number as the number
behind the modulus ( % ) sign fixed the problem, so that the console will output the array. What
I don't understand however is how numberOfValues can be related somehow to the number behind
modulus? Or what is really happening?
Code:
```#include<iostream>
#include<cstdlib>
#include<ctime>

int main()
{
std::srand(std::time(nullptr)); // Set the random seed.
const int numberOfValues = 10;
int valueArray[numberOfValues] = {0};

int counter = numberOfValues;
for(int i = 0; i < numberOfValues; ++i)
{
// Here is the problem when changing the number after
// modulus to another number, sometimes it doesn't work.
valueArray[i] = std::rand() % 5 + 1; //Give a random value between [1, 2000].
//Add checks to make sure that the value is unique

for (int j = 0; j < i; j++) {
if (valueArray[i] == valueArray[j]) {
counter--;
for (int k = i; k < numberOfValues; k++) {
valueArray[k] = valueArray[k + 1];
}
i--;
}
}
}

//Present all values
/* Divide the size of all numbers stored in the array by the size of one number in the array
* An int is 4 bytes which gives me 4 * n, where n is the number of characters in the array.
* Then divide (4 * n) by 4 and we get n. */
int n = sizeof(valueArray) / sizeof(valueArray);
for (int i = 0; i < n; i++) {
std::cout << valueArray[i] << std::endl;
}

getchar();
}
``` 2. The problem is that the modulus has to produce a range of values greater than the size of the array. Think about it. If the array size is 10 and the modulus is 5 then the only number you can produce is a value between 1 and 5. But you need 10 unique values! In other words the loop never completes because the array isn't ever completely filled. 3. Thank you! That is a very clear answer. I understand why that is happening now. Popular pages Recent additions algorithm, array, int, number, problem 