Skip to content

Instantly share code, notes, and snippets.

@Philip803
Created January 11, 2018 19:15
Show Gist options
  • Select an option

  • Save Philip803/e92fb69ceb3a2c792b0919e7209b41ef to your computer and use it in GitHub Desktop.

Select an option

Save Philip803/e92fb69ceb3a2c792b0919e7209b41ef to your computer and use it in GitHub Desktop.
Count numbers of each number created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Count-numbers-of-each-number
function checkNumbers(array){
var counter = 1;
var a = []
for(var i = 1; i <= array.length; i++){
if(array[i] != array[i-1]){
a.push("Number: " + [array[i-1], " Count: " + counter])
counter = 1;
} else {
counter++;
}
}
return a ;
}
console.log("Function 1 :")
console.log(checkNumbers([2,1,1,1,2,2,3,3,3,1,1,5,3,3,1,2,77,6]))
function checkNumbersUnique(array){
var counter = 1;
var a = [];
var b = {};
for(var i = 1 ; i <= array.length; i++){
if(array[i] != array[i-1]){
if(b[array[i-1]]){
b[array[i-1]] += counter
} else {
b[array[i-1]] = counter
}
counter = 1
} else {
counter++;
}
}
a.push(b)
return a;
}
console.log("Function 2: ")
console.log(checkNumbersUnique([2,1,1,1,2,2,3,3,3,1,1,5,3,3,1,2,77,6]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment