如何编写一个函数,返回频率最高的第一个数字(模式)



我正在解决一个问题,该问题要求我返回频率最高的数字(模式(。例如,如果arr包含[3,9,3,1,6],则输出应为3。如果有多个模式,我想返回最先出现的模式[6,6,3,3,5,5]应该返回6,因为它首先出现。如果没有模式,我想返回0。数组不会为空。我是算法的新手,请为我推荐一个更简单的解决方案

function Mode(arr) { 
const arrayObject = {};

arr.forEach(arr => {
if(!arrayObject[arr]) {
arrayObject[arr] = 1
//   console.log(arrayObject)
} else if(arrayObject[arr]){
arrayObject[arr] += 1
//   console.log(arrayObject)
}
})
console.log(arrayObject) // { '3': 2, '5': 2, '6': 2 } array keys are automatically sorted in ascending other.This could be the problem but I don't know how to unsort the arrays//

let highestValueKey = 0;
let highestValue = 0

for(let key in arrayObject) {
const value = arrayObject[key]
if(value > highestValue) {
highestValue = value
highestValueKey = key   
}

}
return Number(highestValueKey)

}
console.log(Mode([6, 6, 3, 3, 5, 5])) // 3 instead of 6

只需记录第一次看到AND时的计数。

function mode (arr) {
const countSeen = {};
const firstSeen = {};
for (let i = 0; i < arr.length; i++) {
let elem = arr[i];
if (! countSeen[elem]) {
countSeen[elem] = 1;
firstSeen[elem] = i;
}
else {
countSeen[elem]++;
}
}
let mostSeenCount = 0;
let mostSeenValue = null;
for (let elem of Object.keys(countSeen)) {
if (mostSeenCount < countSeen[elem] ||
(mostSeenCount == countSeen[elem] && firstSeen[elem] < firstSeen[mostSeenValue])
) {
mostSeenCount = countSeen[elem];
mostSeenValue = elem;
}
}
return mostSeenValue;
}
console.log(mode([5, 6, 6, 6, 3, 3, 5, 5]))

您可以使用一个Map,它的键是数组值,对应的Map值是频率计数:

  • 创建映射并用0初始化计数
  • 访问每个输入值时递增计数
  • 使用Math.max从该映射中获取最大计数
  • 迭代地图以查找与该最大值匹配的第一个计数

当Map条目按插入顺序迭代时,将在最后一步中确定正确的键。

function mode(arr) {
const counts = new Map(arr.map(i => [i, 0]));
for (const i of arr) counts.set(i, counts.get(i) + 1);
const maxCount = Math.max(...counts.values());
for (const [i, count] of counts) {
if (count == maxCount) return i;
}
}
console.log(mode([3, 9, 3, 1, 6])); // 3
console.log(mode([6, 6, 3, 3, 5, 5])); // 6
console.log(mode([3, 9, 3, 6, 1, 9, 9])); // 9
console.log(mode([5, 6, 6, 6, 3, 3, 5, 5])); // 5

您可以使用reduce来计算出现次数,然后从中选择最大值。

function mode(arr) {
const counts = arr.reduce((result, value, index) => {
// result is the object we're building up.
// value is the current item from the array.
// index is value's position within the array

// if result[value] doesn't already exist, create it
result[value] = (result[value] || { index, value, count: 0 });

// increment the 'count' for this value
result[value].count++;

// return the updated result
return result;
}, {});

// sort the entries by count in descending order, then by index
// ascending such that sorted[0] will be the entry with the highest
// count, and lowest index if more that one element has the same count
const sorted = Object.values(counts)
.sort((
{count: ca, index: ia},
{count: cz, index: iz}
) => (cz - ca) || (ia - iz));

// then return that entry's value
return sorted[0].value;
}
console.log(mode([6, 6, 3, 3, 5, 5])) // 6
console.log(mode([3, 9, 3, 1, 6])) // 3
console.log(mode([3, 9, 3, 6, 1, 9, 9])) // 9

最新更新