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:

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check wether
    // an element is present in array or not
      
    import java.util.Arrays;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function return true if given element
        // found in array
        private static void check(int[] arr, int toCheckValue)
        {
            // check if the specified element
            // is present in the array or not
            // using Linear Search method
            boolean test = false;
            for (int element : arr) {
                if (element == toCheckValue) {
                    test = true;
                    break;
                }
            }
      
            // Print the result
            System.out.println("Is " + toCheckValue
                               + " present in the array: " + test);
        }
      
        public static void main(String[] args)
        {
      
            // Get the array
            int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
      
            // Get the value to be checked
            int toCheckValue = 7;
      
            // Print the array
            System.out.println("Array: "
                               + Arrays.toString(arr));
      
            // Check if this value is
            // present in the array or not
            check(arr, toCheckValue);
        }
    }

    chevron_right

    
    

    Output:

    Array: [5, 1, 1, 9, 7, 2, 6, 10]
    Is 7 present in the array: true
    
  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check wether
    // an element is present in array or not
      
    import java.util.Arrays;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function return true if given element
        // found in array
        private static void check(int[] arr, int toCheckValue)
        {
            // sort given array
            Arrays.sort(arr);
      
            // check if the specified element
            // is present in the array or not
            // using Binary Search method
            int res = Arrays.binarySearch(arr, toCheckValue);
      
            boolean test = res > 0 ? true : false;
      
            // Print the result
            System.out.println("Is " + toCheckValue
                               + " present in the array: " + test);
        }
      
        public static void main(String[] args)
        {
      
            // Get the array
            int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
      
            // Get the value to be checked
            int toCheckValue = 7;
      
            // Print the array
            System.out.println("Array: "
                               + Arrays.toString(arr));
      
            // Check if this value is
            // present in the array or not
            check(arr, toCheckValue);
        }
    }

    chevron_right

    
    

    Output:

    Array: [5, 1, 1, 9, 7, 2, 6, 10]
    Is 7 present in the array: true
    
  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check wether
    // an element is present in array or not
      
    import java.util.Arrays;
      
    class GFG {
      
        // Function return true if given element
        // found in array
        private static void check(Integer[] arr, int toCheckValue)
        {
            // check if the specified element
            // is present in the array or not
            // using contains() method
            boolean test
                = Arrays.asList(arr)
                      .contains(toCheckValue);
      
            // Print the result
            System.out.println("Is " + toCheckValue
                               + " present in the array: " + test);
        }
      
        public static void main(String[] args)
        {
      
            // Get the array
            Integer arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
      
            // Get the value to be checked
            int toCheckValue = 7;
      
            // Print the array
            System.out.println("Array: "
                               + Arrays.toString(arr));
      
            // Check if this value is
            // present in the array or not
            check(arr, toCheckValue);
        }
    }

    chevron_right

    
    

    Output:



    Array: [5, 1, 1, 9, 7, 2, 6, 10]
    Is 7 present in the array: true
    
  4. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check wether
    // an element is present in array or not
      
    import java.util.Arrays;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function return true if given element
        // found in array
        private static void check(int[] arr, int toCheckValue)
        {
            // check if the specified element
            // is present in the array or not
            // using anyMatch() method
            boolean test
                = IntStream.of(arr)
                      .anyMatch(x -> x == toCheckValue);
      
            // Print the result
            System.out.println("Is " + toCheckValue
                               + " present in the array: " + test);
        }
      
        public static void main(String[] args)
        {
      
            // Get the array
            int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
      
            // Get the value to be checked
            int toCheckValue = 7;
      
            // Print the array
            System.out.println("Array: "
                               + Arrays.toString(arr));
      
            // Check if this value is
            // present in the array or not
            check(arr, toCheckValue);
        }
    }

    chevron_right

    
    

    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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check wether
    // an element is present in array or not
      
    import java.util.Arrays;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function return true if given element
        // found in array
        private static void check(int[] arr, int toCheckValue)
        {
            // check if the specified element
            // is present in the array or not
            // using anyMatch() method
            boolean test
                = IntStream.of(arr)
                      .anyMatch(x -> x == toCheckValue);
      
            // Print the result
            System.out.println("Is " + toCheckValue
                               + " present in the array: " + test);
        }
      
        public static void main(String[] args)
        {
      
            // Get the array
            int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
      
            // Get the value to be checked
            int toCheckValue = 7;
      
            // Print the array
            System.out.println("Array: "
                               + Arrays.toString(arr));
      
            // Check if this value is
            // present in the array or not
            check(arr, toCheckValue);
        }
    }

    chevron_right

    
    

    Output:

    Array: [5, 1, 1, 9, 7, 2, 6, 10]
    Is 7 present in the array: true
    


  5. My Personal Notes arrow_drop_up

    Just another competitive programmer and hard worker

    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.