B站前端笔试题 20210913
注:如果不存在第n多的,则返回-1
let arr=[1,3,2,3,3,2,2,1,2]//[1,3,2,3,2,2,1,2] [1,3,2,3,2,2,1,2] 4
let n=3;
let obj={};
for(let i = 0;i < arr.length; i++){
let item = arr[i];
obj[item] = (obj[item] + 1) || 1;
}
const count = [];
for(let key in obj){
count.push(obj[key]);
}
count.sort();
let c = -1;
if(n <= count.length){
c = count[count.length-n];
}
const nums = [];
for(let key in obj){
if(c === obj[key]){
nums.push(key);
}
}
let num = -1;
if(nums.length !== 0){
num = nums.sort()[0];
}
console.log(num);
console.log(c);