Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Last active July 26, 2019 23:43
Show Gist options
  • Save brianswisher/2cc93b300da9647395b8eebd4cd7361a to your computer and use it in GitHub Desktop.
Save brianswisher/2cc93b300da9647395b8eebd4cd7361a to your computer and use it in GitHub Desktop.
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
Copy link
Author

brianswisher 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