Last active
July 26, 2019 23:43
-
-
Save brianswisher/2cc93b300da9647395b8eebd4cd7361a to your computer and use it in GitHub Desktop.
Count Unique Values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(()=>{ | |
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) | |
})() |
Author
brianswisher
commented
Jul 25, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment