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);}}Output: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);}}Output: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);}}Output: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);}}Output: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);}}Output:Array: [5, 1, 1, 9, 7, 2, 6, 10] Is 7 present in the array: true
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



