Created
November 27, 2017 04:17
-
-
Save binario200/466a45ee443adcf6a224386dc0fdf0d9 to your computer and use it in GitHub Desktop.
gets the top three numbers in an array
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
var input = [6,7,3,8,2,5,8,9,2,3,3,1,4,5,5,5,2,3,5]; | |
console.log(input.join(' ')); | |
function top(arr) { | |
var sum = {}; | |
var temp = arr; | |
var top = [null, null, null]; | |
var first = 0; | |
var second = 0; | |
var third = 0; | |
while (temp.length > 0) { | |
var current = temp[0]; | |
if (!sum[current]) { | |
var count = 0; | |
count = summary(temp, current); | |
temp = remaining(temp, current); | |
//console.log('remainig ' + temp); | |
sum[current] = count; | |
let st = top[0]; | |
let sd = top[1]; | |
let th = top[2]; | |
//console.log(st, sd, th); | |
if (count > first) { | |
second = first; | |
third = second; | |
first = count; | |
top[2] = sd; | |
top[1] = st; | |
top[0] = current; | |
} else if (count > second) { | |
third = second; | |
second = count; | |
top[2] = sd; | |
top[1] = current; | |
} else if (count > third) { | |
third = count; | |
top[2] = current; | |
} | |
} else { | |
temp = []; | |
} | |
} | |
console.log(sum); | |
console.log(top); | |
//ponderiza(top); | |
} | |
function remaining(arr, curr) { | |
var newArr = []; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] != curr) { | |
newArr.push(arr[i]); | |
} | |
} | |
return newArr; | |
} | |
function summary(arr, curr) { | |
var count = 0; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] == curr) { | |
count += 1; | |
} | |
} | |
return count; | |
} | |
top(input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment