Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

The sequential search

The sequential search or linear search is the most basic search algorithm. It consists of comparing each element of the data structure with the one we are looking for. It is also the most inefficient one.

Let's take a look at its implementation:

const DOES_NOT_EXIST = -1;function sequentialSearch(array, value, equalsFn = defaultEquals) {  for (let i = 0; i < array.length; i++) { // {1}    if (equalsFn(value, array[i])) { // {2}      return i; // {3}    }  }  return DOES_NOT_EXIST; // {4}} 

The sequential search iterates through the array ({1}) and compares each value with the value we are searching for ({2}). If we find it, we can return something to indicate that we found it. We can return the value itself, the value true, or its ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content