Open In App

Check if a value is present in an Array in Java

Last Updated : 09 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, the task is to write a Java program to check whether a specific element is present in this Array or not.

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

An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be quickly sorted or searched. All the items of the array are stored at contiguous memory locations. 

Approaches

There are numerous approaches to check whether a specific element is present in this Array or not in Java. These are – 

  • Using the Linear Search method
  • Using the Binary Search method
  • Using List.contains() method
  • Using Stream.anyMatch() method

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: 

Java




// Java program to check whether
// 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);
    }
}


Output

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

Complexity of the above method:

Time Complexity: O(N)
Auxiliary Space: O(1)

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, the Arrays.binarySearch() method is used for Binary Search.

Syntax: 

public static int 
    binarySearch(data_type arr, data_type key)

Example:

Java




// Java program to check whether
// 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);
    }
}


Output

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

Complexity of the above method:

Time Complexity: O(nlog(n))
Auxiliary Space: O(1)

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: 

Java




// Java program to check whether
// 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);
    }
}


Output

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

Complexity of the above method:

Time Complexity: O(N)
Auxiliary Space: O(1)

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

Java




// Java program to check whether
// 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);
    }
}


Output

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

Complexity of the above method:

Time Complexity: O(N)
Auxiliary Space: O(1)

Example 2: Using Arrays.stream() method to create Stream

Java




// Java program to check whether
// 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
            = Arrays.stream(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);
    }
}


Output

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

Complexity of the above method:

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads