Check if an array contains duplicate values
javascript
Solution
You got the return values the wrong way round:
As soon as you find two values that are equal, you can conclude that the array is not unique and return `false`.
At the very end, after you've checked all the pairs, you can return `true`.
If you do this a lot, and the arrays are large, you might want to investigate the possibility of sorting the array and then only comparing adjacent elements. This will have better asymptotic complexity than your current method.
Problem
I wanted to write a javascript function which checks if array contains duplicate values or not. I have written the following code but its giving answer as "true" always. Can anybody please tell me what am I missing. ``` function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray.length; i++) { for (var j = 0; j < myArray.length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. } ```