跳到主要内容

顺序查找

实现

:::details 点击查看代码

function SequenceSearch(array, value) {
for (let i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}

function SequenceSearchAll(array, value) {
const result = [];
for (let i = 0; i < array.length; i++) {
if (array[i] == value) {
result.push(i);
}
}
return result;
}
const array = [10, 20, 30, 40, 20, 50, 60];
console.log(SequenceSearchAll(array, 20));

:::