From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Search array-like structures

Search array-like structures

- We were able to access a specific item in a list and a tuple because we already knew its index. We knew where it was stored. When you're searching for an item, you don't know its index or if it even exists in the array. So how can we search for an item? Well, in searching, we usually want to return a true or false value depending on if the value exists in the array. Sometimes, if the item is in the array, we'll also want to know its index and even retrieve the item. Now, the most obvious solution for searching is to check every item, to go through the entire data structure and check if the item at a given index matches the item we're looking for. If there's a match, we return true. If we've gone through the entire data structure and there is not a match, we return false. To do this in most languages, we can use a for loop, iterating over the data structure and checking each item at every index to see if it…

Contents