mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
23 lines
548 B
JavaScript
23 lines
548 B
JavaScript
import Comparator from '../../../utils/comparator/Comparator';
|
|
|
|
/**
|
|
* Linear search implementation.
|
|
*
|
|
* @param {*[]} array
|
|
* @param {*} seekElement
|
|
* @param {function(a, b)} [comparatorCallback]
|
|
* @return {number[]}
|
|
*/
|
|
export default function linearSearch(array, seekElement, comparatorCallback) {
|
|
const comparator = new Comparator(comparatorCallback);
|
|
const foundIndices = [];
|
|
|
|
array.forEach((element, index) => {
|
|
if (comparator.equal(element, seekElement)) {
|
|
foundIndices.push(index);
|
|
}
|
|
});
|
|
|
|
return foundIndices;
|
|
}
|