Function linearSearch

  • Performs a linear search on the data array.

    • Iterates through the array and returns the index of the first occurrence of target.
    • Uses a custom comparison function to determine equality.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • data: T[]

      The array in which to search for the target.

    • target: T

      The target value to find within the array.

    • compareFn: ((a: T, b: T) => number)

      A comparison function that compares two elements. Should return 0 if a and b are considered equal, a negative number if a < b, and a positive number if a > b.

        • (a, b): number
        • Parameters

          Returns number

    Returns number

    The index of the target in the array, or -1 if the target is not found.

    // Basic usage with an array of numbers
    const data = [10, 20, 30, 40];
    const index = linearSearch(data, 30, (a, b) => a - b);
    // index would be 2
    // Usage with an array of strings
    const data = ['apple', 'banana', 'cherry'];
    const index = linearSearch(data, 'banana', (a, b) => a.localeCompare(b));
    // index would be 1