UNPKG

@dxzmpk/js-algorithms-data-structures

Version:

Algorithms and data-structures implemented on JavaScript

23 lines (19 loc) 548 B
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; }