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!
Can you sort the arr
first?
std::sort(std::begin(arr), std::end(arr));
auto pos = std::adjacent_find(std::begin(arr), std::end(arr));
if (pos != std::end(arr))
// we have a duplicate
the first for-loop is wrong. There's a j
instead of an i
for (unsigned i = 0; i < size; i++)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With