Check if a value is present in an Array in Java
Given an array, the task is to check whether a certain element is present in this Array or not, in Java.
Examples:
Input: arr[] = [5, 1, 1, 9, 7, 2, 6, 10], key = 7 Output: true Input: arr[] = [-1, 1, 5, 8], key = -2 Output: false
Below are various ways to do so:
- Using Linear Search Method:
In this, the list or array is traversed sequentially and every element is checked.Syntax:
for (int element : arr) { if (element == toCheckValue) { return true; } }Example:
// Java program to check wether// an element is present in array or notimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function return true if given element// found in arrayprivatestaticvoidcheck(int[] arr,inttoCheckValue){// check if the specified element// is present in the array or not// using Linear Search methodbooleantest =false;for(intelement : arr) {if(element == toCheckValue) {test =true;break;}}// Print the resultSystem.out.println("Is "+ toCheckValue+" present in the array: "+ test);}publicstaticvoidmain(String[] args){// Get the arrayintarr[] = {5,1,1,9,7,2,6,10};// Get the value to be checkedinttoCheckValue =7;// Print the arraySystem.out.println("Array: "+ Arrays.toString(arr));// Check if this value is// present in the array or notcheck(arr, toCheckValue);}}chevron_rightfilter_noneOutput:Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
- Using Binary Search Method:
In this, search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.In this example, Arrays.binarySearch() method is used for Binary Search.
Syntax:
public static int binarySearch(data_type arr, data_type key)Example:
// Java program to check wether// an element is present in array or notimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function return true if given element// found in arrayprivatestaticvoidcheck(int[] arr,inttoCheckValue){// sort given arrayArrays.sort(arr);// check if the specified element// is present in the array or not// using Binary Search methodintres = Arrays.binarySearch(arr, toCheckValue);booleantest = res >0?true:false;// Print the resultSystem.out.println("Is "+ toCheckValue+" present in the array: "+ test);}publicstaticvoidmain(String[] args){// Get the arrayintarr[] = {5,1,1,9,7,2,6,10};// Get the value to be checkedinttoCheckValue =7;// Print the arraySystem.out.println("Array: "+ Arrays.toString(arr));// Check if this value is// present in the array or notcheck(arr, toCheckValue);}}chevron_rightfilter_noneOutput:Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
- Using List.contains() Method: List contains() method in Java is used for checking if the specified element exists in the given list or not.
Syntax:
public boolean contains(Object) where object-element to be searched for.
Example:
// Java program to check wether// an element is present in array or notimportjava.util.Arrays;classGFG {// Function return true if given element// found in arrayprivatestaticvoidcheck(Integer[] arr,inttoCheckValue){// check if the specified element// is present in the array or not// using contains() methodbooleantest= Arrays.asList(arr).contains(toCheckValue);// Print the resultSystem.out.println("Is "+ toCheckValue+" present in the array: "+ test);}publicstaticvoidmain(String[] args){// Get the arrayInteger arr[] = {5,1,1,9,7,2,6,10};// Get the value to be checkedinttoCheckValue =7;// Print the arraySystem.out.println("Array: "+ Arrays.toString(arr));// Check if this value is// present in the array or notcheck(arr, toCheckValue);}}chevron_rightfilter_noneOutput:
Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
- Using Stream.anyMatch() Method:
Stream anyMatch(Predicate predicate) returns whether any elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determining the result.Syntax:
boolean anyMatch(Predicate<T> predicate) Where T is the type of the input to the predicate and the function returns true if any elements of the stream match the provided predicate, otherwise false.
Example 1: Using Stream.of() method to create Stream
// Java program to check wether// an element is present in array or notimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function return true if given element// found in arrayprivatestaticvoidcheck(int[] arr,inttoCheckValue){// check if the specified element// is present in the array or not// using anyMatch() methodbooleantest= IntStream.of(arr).anyMatch(x -> x == toCheckValue);// Print the resultSystem.out.println("Is "+ toCheckValue+" present in the array: "+ test);}publicstaticvoidmain(String[] args){// Get the arrayintarr[] = {5,1,1,9,7,2,6,10};// Get the value to be checkedinttoCheckValue =7;// Print the arraySystem.out.println("Array: "+ Arrays.toString(arr));// Check if this value is// present in the array or notcheck(arr, toCheckValue);}}chevron_rightfilter_noneOutput:Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
Example 2: Using Arrays.stream() method to create Stream
// Java program to check wether// an element is present in array or notimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function return true if given element// found in arrayprivatestaticvoidcheck(int[] arr,inttoCheckValue){// check if the specified element// is present in the array or not// using anyMatch() methodbooleantest= IntStream.of(arr).anyMatch(x -> x == toCheckValue);// Print the resultSystem.out.println("Is "+ toCheckValue+" present in the array: "+ test);}publicstaticvoidmain(String[] args){// Get the arrayintarr[] = {5,1,1,9,7,2,6,10};// Get the value to be checkedinttoCheckValue =7;// Print the arraySystem.out.println("Array: "+ Arrays.toString(arr));// Check if this value is// present in the array or notcheck(arr, toCheckValue);}}chevron_rightfilter_noneOutput:Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
- Queries to check whether a given digit is present in the given Range
- Check if the given string of words can be formed from words present in the dictionary
- Java program to read all mobile numbers present in given file
- Check if it is possible to sort the array after rotating it
- Check if all elements of the array are palindrome or not
- Find all reachable nodes from every node present in a given set
- Check if the array has an element which is equal to product of remaining elements
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1
- Check if URL is valid or not in Java
- How to check if a key exists in a HashMap in Java
- Check if a File is hidden in Java
- Check for string rotation in Java
- Check if a string contains only alphabets in Java using Regex
- Check if email address valid or not in Java
- Check if a String starts with any of the given prefixes in Java
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



