Skip to Content
A Common-Sense Guide to Data Structures and Algorithms
book

A Common-Sense Guide to Data Structures and Algorithms

by Jay Wengrow
August 2017
Intermediate to advanced
222 pages
5h 3m
English
Pragmatic Bookshelf
Content preview from A Common-Sense Guide to Data Structures and Algorithms

A Quadratic Problem

Let’s say you’re writing a JavaScript application that requires you to check whether an array contains any duplicate values.

One of the first approaches that may come to mind is the use of nested for loops, as follows:

 function​ hasDuplicateValue(array) {
 for​(​var​ i = 0; i < array.length; i++) {
 for​(​var​ j = 0; j < array.length; j++) {
 if​(i !== j && array[i] == array[j]) {
 return​ ​true​;
  }
  }
  }
 return​ ​false​;
 }

In this function, we iterate through each element of the array using var i. As we focus on each element in i, we then run a second for loop that checks through all of the elements in the array—using var j—and checks if the elements at positions i and j are the same. If they are, that ...

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

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

Jay Wengrow

Publisher Resources

ISBN: 9781680502794Errata Page