Pollo Pollo - 26 days ago 9
C++ Question

C++ How to check if all values in array are different?

Hello I'm trying to check any values that are inputted in an array of any size are different. I am trying to use a nested loop for this code but cannot get a proper if statement to check that each value in the array are different. I'd appreciate any help!

for (unsigned i = 0; i < size; i++)
for (unsigned k = i + 1; k < size; k++)
if (arr[i] == arr[k]){
return false;
}
return true;


Ok thank you guys for the help your suggestions worked!

Answer

There are two problems in your code:

 1. first loop should have j instead if i.

2. The second loop should start from i+1.

Hope this helps.