Given an array of integers and a key element, the task is to check whether the key is present in the array. If the key exists, the method returns true; otherwise, it returns false.
- All methods return a boolean result when used as shown above.
- For a single search in a small unsorted array, Linear Search is usually the simplest choice.
Illustration:
Input: arr[] = [3, 5, 7, 2, 6, 10], key = 7
Output: Is 7 present in the array: true
Input: arr[] = [-1, 1, 5, 8], key = -2
Output: Is -2 present in the array: false
Methods to check If a Value is Present in an Array
1. Using Linear Search Method
In the Linear Search method, each element of the array is sequentially compared with the key until a match is found or the end of the array is reached.
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
for (int element : arr) {
if (element == key) {
return true;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
Output
Is 7 present in the array: true
Explanation:
- Create a method isElementPresent which return a boolean value and takes two parameters array (arr) and the element(key).
- Iterate each element of the array and check if the element is equal to the given key return true otherwise return false.
2. Using Binary Search Method
Binary Search efficiently finds an element in a sorted array by repeatedly dividing the search interval in half. The built-in Arrays.binarySearch() method performs this operation in logarithmic time.
Syntax:
public static int binarySearch(data_type[] arr, data_type key)
Return Value: Returns the index of the key if found; otherwise, a negative value.
Parameters:
- array: The array to be searched.
- key: The value to be searched for.
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
Arrays.sort(arr); // Binary search works only on sorted arrays
int res = Arrays.binarySearch(arr, key);
return res >= 0;
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 17;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
Output
Is 17 present in the array: false
Explanation: The array is first sorted because binary search requires sorted data. The method then searches for 17. Since 17 is not present, Arrays.binarySearch() returns a negative value, so the method returns false.
3. Using List.contains() Method
The contains() method of the List interface checks if a specific element exists in a list. We can convert an array to a list using Arrays.asList().
Syntax:
public boolean contains(Object element)
- Parameter: It takes a single parameter Object which to be searched in the given list.
- Return Type: It return a boolean value, if the element is present in the list return true, otherwise return false.
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(Integer[] arr, int key) {
return Arrays.asList(arr).contains(key);
}
public static void main(String[] args) {
Integer[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
Output
Is 7 present in the array: true
Explanation: Arrays.asList(arr) converts the Integer[] into a List. The contains() method then checks whether 7 exists in the list. Since 7 is present, it returns true.
4. Using Stream.anyMatch() Method
The anyMatch() method checks whether any element in a stream matches the provided predicate. It short-circuits as soon as a match is found.
Syntax:
boolean anyMatch(Predicate<T> predicate)
- Parameter: This method takes a single parameter predicate of type T which is a generic.
- Return Types: This method return a boolean value, True if the element is present otherwise return false.
Example: Using IntStream.of()
import java.util.stream.IntStream;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
return IntStream.of(arr).anyMatch(x -> x == key);
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
Output
Is 7 present in the array: true
Explanation: IntStream.of(arr) creates an IntStream from the array. The anyMatch() method checks whether any element is equal to 7. When it finds 7, it returns true without checking the remaining elements.
Example: Using Arrays.stream() method to create Stream
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
return Arrays.stream(arr).anyMatch(x -> x == key);
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
Output
Is 7 present in the array: true
Explanation: Arrays.stream(arr) creates an IntStream from the integer array. The anyMatch() method checks each element against the condition x == key. Since 7 is found, it returns true.