Introduction To Array's Linear Search Methods.

Array is an important data type in most programming languages. As programmers, we make use of arrays frequently.

Array provides a lot of methods. We will be looking at the methods for linearly or sequentially searching item(s) in an array.

Linear search or sequential search is a method of finding an item within a list. It checks the list for the item being searched for until it is found or the list is exhausted.

Linear search is best suited for a short list. This is because the worst-case complexity is O(n). This happens when the item searched for is not in the list.

Array Linear Search Methods

Let's create an array of numbers we would use in the course of this article.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5]

indexOf:

This method returns the first index at which the item being searched for is found or -1 if the item is not found.

/**
 * @param {element to find in the array}
 * @return number
*/
list.indexOf(5);
// 4
list.indexOf(12);
// -1

includes:

This method checks if an item is in an array and returns true if found and false otherwise.

/**
 * @param {element to find in the array}
 * @return Boolean
*/
list.includes(5);
// true
list.includes(12);
// false

lastIndexOf:

This method returns the last index at which the item being searched for is found or -1 if the item is not found.

/**
 * @param {element to find in the array}
 * @return number
*/
list.lastIndexOf(5)
// 10
list.lastIndexOf(12)
// -1

find:

This method returns the value of the first item that satisfies the provided testing function. If no item satisfies the testing function undefined is returned.

/**
 * @param funtion {provided test function}
 * @return number | undefined
*/
list.find(item => item > 5)
// 6
list.find(item => item == 0)
// undefined

findIndex:

This method returns the index of the first item that satisfies the provided testing function. If no item satisfies the testing function -1 is returned.

/**
 * @param funtion {provided test function}
 * @return number
*/
list.findIndex(item => item > 5);
// 5
list.findIndex(item => item == 0);
// -1

filter:

This method creates an array with all the items that satisfy the provided testing function. If no item is found it returns an empty array.

/**
 * @param funtion {provided test function}
 * @return Array
*/
list.filter(item => item > 5)
// [ 6, 7, 8, 9, 10 ]
list.filter(item => item == 0)
// [ ]

every:

This method checks if all items in an array satisfy the provided testing function. It returns true if all items passed the test and false otherwise.

/**
 * @param funtion {provided test function}
 * @return Boolean
*/
list.every(item => item > 5);
// false
list.every(item => item > 0);
// true

some:

This method checks if an item in an array satisfies the provided testing function. It returns true if at least one item passed the test and false otherwise.

/**
 * @param funtion {provided test function}
 * @return Boolean
*/
list.some(item => item > 5);
// true
list.some(item => item == 0);
// false

Summary

As we have seen above, there are different methods that we can use to search for items in an array depending on our use case. However, these methods might not work best for a large list. There are other search algorithms that can be used in place of these methods. You can learn more about each method from the links below:

Searching in Array
Javascript Array Methods