Check if item is in an array / list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When working with arrays or lists in programming, a common requirement is to check if a specific item exists within the array or list. This operation is fundamental and used widely across different programming tasks, from validating inputs to managing collections of data. This detailed article explores various methods to check for the presence of an item in an array or list across several popular programming languages.
Basic Concepts
Arrays and lists are data structures used to store collections of elements. An array is typically a fixed-size, ordered collection of elements of the same type, while a list is more flexible, often allowing for dynamic sizing and various data types. Despite these differences, the operation of checking if an item exists remains similar.
Common Approaches
- Iterative Method: Loop through each element using a for or while loop to check if it matches the desired item.
- Built-in Functions: Utilizing language-specific functions, which are optimized for performance, to check the presence of an item.
- Set Conversion: Convert the list to a set and check membership, leveraging the efficiency of hash tables.
Let's explore these approaches in detail, using pseudo-code and examples from different languages:
Methods and Examples
Iterative Method
The iterative method involves looping through each element in the list and comparing each element with the target:
- Python: Using the `in` keyword.
- JavaScript: Using `includes()` method.
- Java: Using `contains()` method in collections like `ArrayList`.
- Iterative Method: complexity as it may need to check each element.
- Built-in Functions: Often optimized with to complexity depending on internal implementation.
- Set Conversion: This approach offers complexity for lookups after the initial conversion to a set.
- Data Type Compatibility: Ensure that the item and the elements of the list are comparable.
- Null and Undefined Values: Be mindful of elements that are `None` (Python), `null` (Java/ JavaScript), or `undefined` (JavaScript) to avoid logical errors.
- Handling Duplicates: If your task requires counting occurrences, different logic or methods (e.g., collections counter) may be needed.
Related reading
- Check if list contains element that contains a string and get that element
- Check if one list contains element from the other
- Check if optional array is empty
- Check if optional array is empty
- Check synchronously if file/directory exists in Node.js
- Check whether a string matches a regex in JS
- Check if something is not in a list in Python
- Check if sum possible in array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.