Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Count Unique Values
(()=>{
function countUniqueValues(sortedArray) {
const myArray = sortedArray.slice(0)
let count = 0
myArray.forEach((num, i) => {
const rightNum = myArray[i + 1]
if (myArray[count] !== rightNum) {
count++
if (rightNum !== undefined) {
myArray[count] = rightNum
}
}
})
return count
}
const sArray = [1,1,2,2,3,3,4,5]
const uValCount = countUniqueValues(sArray)
console.log(uValCount)
console.log(sArray)
})()
@brianswisher

This comment has been minimized.

Copy link
Owner Author

commented Jul 25, 2019

// Can we mutate the input? YES

// Set up left pointer

// Loop input
  // Set up  right number

  // Do nothing, unless left & right do not match
    // Move left forward

    // If right is defined
      // update input's left index with the right number


// return left pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.