Created
January 31, 2023 13:42
-
-
Save Chinecherem20199/d79d9e2849252b1e7172930848d7afce to your computer and use it in GitHub Desktop.
Count Unique array in JavaScript DSA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countUniqueValues(arr){ | |
if(arr.length ===0) return 0; | |
let i = 0; | |
for(j =1; j<arr.length; j++){ | |
if (arr[i] !== arr[j]) { | |
i++; | |
arr[i] = arr[j]; | |
} | |
} | |
return i + 1 | |
} | |
countUniqueValues([1,1,1,1,1,2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment