Skip to content

Instantly share code, notes, and snippets.

@Chinecherem20199
Created January 31, 2023 13:42
Show Gist options
  • Save Chinecherem20199/d79d9e2849252b1e7172930848d7afce to your computer and use it in GitHub Desktop.
Save Chinecherem20199/d79d9e2849252b1e7172930848d7afce to your computer and use it in GitHub Desktop.
Count Unique array in JavaScript DSA
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