Skip to content

Instantly share code, notes, and snippets.

@MacTanomsup
Created November 17, 2018 06:36
Show Gist options
  • Select an option

  • Save MacTanomsup/fb0095f5c67c5545678ba9e23cbaecd0 to your computer and use it in GitHub Desktop.

Select an option

Save MacTanomsup/fb0095f5c67c5545678ba9e23cbaecd0 to your computer and use it in GitHub Desktop.
function countUniqueValues(sortedValue){
if(sortedValue.length === 0) {
return 0;
}
var left = 0;
var right = 1;
while(right < sortedValue.length) {
if(sortedValue[left] === sortedValue[right]) {
right++;
} else if(sortedValue[left] !== sortedValue[right]) {
if(right - left > 1) {
sortedValue[++left] = sortedValue[right];
} else {
left++;
right++;
}
}
}
return left + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment