In this article, we are going to learn how to check if a value is present in an array or not. To do so we will require the array to search in and the target element whose presence has to be checked.
JavaScript Arrays are used to store a list of elements that can be accessed by a single variable.
Once we have a target element we can perform any of the search algorithms to check for the presence of the element in the array.
1. Linear Search Algorithm (Naive approach):
In the Linear search algorithm, we compare each element of the array with the target. 8 is part of the num array below.
Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function check(element) { for (let i = 0; i < num.length; i++) { if (num[i] == element) return element + " is present in the array." ; } return element + " is not present in the array." ; } console.log(check(8)); |
8 is present in the array.
Time complexity: O(n)
The time complexity for this algorithm is O(n) as we are looping through the array once to check for the given element.
Space complexity: O(1)
The space complexity for this algorithm is O(1) as we are not using any additional space other than the input array.
2. Using indexOf() function:
The indexOf() function returns the index of the target element in the array if it is present and -1 if not present.
For example, 41 is not part of the num array in the code below.
Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let element = 41; if (num.indexOf(element) > 0) console.log(element + " is present." ); else console.log(element + " is not present." ); |
41 is not present.
Time complexity: O(n)
Space complexity: O(1)
3. Binary Search:
The Binary search algorithm works only on sorted arrays and keeps dividing the array into 2 equal halves and works recursively.
Javascript
function bsearch(arr, l, r, x) { if (r >= l) { let mid = l + Math.floor((r - l) / 2); if (arr[mid] == x) return mid; if (arr[mid] > x) return bsearch(arr, l, mid - 1, x); return bsearch(arr, mid + 1, r, x); } return -1; } let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // To check if 85 is present or not console.log( "Is 85 present? " + (bsearch(num, 0, num.length, 85) != -1)); // To check if 1 is present or not console.log( "Is 1 present? " + (bsearch(num, 0, num.length, 1) != -1)); |
Is 85 present? false Is 1 present? true
Time Complexity: O(log n)
Space Complexity: O(1)
4. filter() Method:
The filter() method is used with the array to pull out the desired element from the array. We first create the array and use the filter method on an array with a method that checks element is present or not. If the element is present in the array it returns an array with an element else returns an empty array.
Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function check(element) { let ans = num.filter(x => x == element); if (ans.length) return element + " is present in the array." ; return element + " is not present in the array." ; } console.log(check(81)); |
81 is not present in the array.
Time Complexity: O(N). Here N is the size of an array.
Space Complexity: O(1). Because it does not require extra memory.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!