Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created May 21, 2021 21:20
Show Gist options
  • Save misterpoloy/c890832ac7d3f77593a5d8a0b42e97d9 to your computer and use it in GitHub Desktop.
Save misterpoloy/c890832ac7d3f77593a5d8a0b42e97d9 to your computer and use it in GitHub Desktop.
2 pointers pattern: Count Unique values
function countUniqueValues(numbers){
// add whatever parameters you deem necessary - good luck!
if (!numbers.length) return 0;
let left = 0;
let right = 1;
let counter = 1;
while (right < numbers.length) {
if (numbers[left] !== numbers[right]) {
left = right;
counter++;
}
right++;
}
return counter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment