js 数组中超过三个或三个以上相同的数字,返回其索引
function findRepeatedIndexes(arr) {
const repeatedIndexes = {};
arr.forEach((num, index) => {
if (repeatedIndexes[num]) {
repeatedIndexes[num].push(index);
} else {
repeatedIndexes[num] = [index];
}
});
const result = [];
Object.keys(repeatedIndexes).forEach(key => {
if (repeatedIndexes[key].length >= 3) {
result.push(repeatedIndexes[key]);
}
});
return result;
}
// 使用
findRepeatedIndexes([1,2,1,1,5]) //输出 [0, 2, 3]