Check If a Value is Present in an Array in Java (original) (raw)

Last Updated : 15 Apr, 2025

Given an array of Integers and a key element. Our task is to check whether the key element is present in the array. If the element (key) is present in the array, return true; otherwise, return false.

**Example:

**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

Approaches

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

**1. Using Linear Search Method

In Linear Search, the list or array is traversed sequentially, and every element is checked.

**Syntax:

for (int element : arr) {
if (element == toCheckValue) {
return true;
}
}

**Example: Check whether the key is present in the array using Linear search in Java.

Java `

// Java program to check whether // an element is present in array or not import java.util.Arrays; import java.util.stream.IntStream;

class Geeks { // Method to check if the specified element // is present in the array or not private static boolean isElementPresent(int[] arr, int key) { // check if the specified element // is present in the array or not // using Linear Search method for (int element : arr) { if (element == key) { return true; } }

    return false;
}

public static void main(String[] args)
{

    // Get the array
    int arr[] = { 3, 5, 7, 2, 6, 10 };

    // Get the value to be checked
    int key = 7;

    // Check if this value is
    // present in the array or not
    boolean res = isElementPresent(arr, key);

    System.out.println("Is " + key
                       + " present in the array: "
                       + res);
    
}

}

`

Output

Is 7 present in the array: true

**Explanation: In the above code example, we create a method **isElementPresent which return a boolean value. Which takes two parameters array (arr) and the element(key). We iterate each element of the array and check if the element is equal to the given key return true otherwise return false.

**Complexity Analysis:

**2. Using Binary Search Method

In Binary Search Method, 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)

**Note: Here datatype can be any of the primitive data types such as byte, char, double, int, float, short, long, and even object as well.

**Parameters:

**Return Type: It returns the index of the key, if the index is found otherwise it returns -1.

**Example: Check whether the key is present in the array using in-built Binary search method in Java.

Java `

// Java program to check whether // an element is present in array or not import java.util.Arrays; import java.util.stream.IntStream;

class Geeks { // Method to check if the specified element // is present in the array or not private static boolean isElementPresent(int[] arr, int key) { // 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, key);

    // if the element is not present in the array
    // then the result will be negative
    // so we will return false
    if (res < 0)
        return false;
 
    // otherwise element is present in the array
        return true;
}

public static void main(String[] args)
{

    // Get the array
    int arr[] = { 3, 5, 7, 2, 6, 10 };

    // Get the value to be checked
    int key = 17;

    // Check if this value is
    // present in the array or not
    boolean res = isElementPresent(arr, key);


    System.out.println("Is " + key
                       + " present in the array: "
                       + res);
    
}

}

`

Output

Is 17 present in the array: false

**Explanation: In the above code example, we use the binary search method to find the whether the element is present in the array. Binary search is only works for the sorted array so first we sort the array. The method returns the index of the element if it is present in the array otherwise it returns -1.

**Complexity Analysis:

**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)

**Example: Check whether the key is present in the array using List contains() method in Java.

Java `

// Java program to check whether // an element is present in array or not import java.util.Arrays; import java.util.stream.IntStream;

class Geeks { // Method to check if the specified element // is present in the array or not private static boolean isElementPresent(Integer[] arr, int key) { // check if the specified element // is present in the array or not // using contains() method

    boolean res = Arrays.asList(arr)
              .contains(key);
              
   // Return the result
    return res;
}

public static void main(String[] args)
{

    // Get the array
    Integer arr[] = { 3, 5, 7, 2, 6, 10 };

    // Get the value to be checked
    int key = 7;

    // Check if this value is
    // present in the array or not
    boolean res = isElementPresent(arr, key);

    System.out.println("Is " + key
                       + " present in the array: "
                       + res);
    
}

}

`

Output

Is 7 present in the array: true

**Explanation: In the above code example, we use the **contains(Object) which works on the list. Using this method we can check whether the element is present in the array.

**Complexity of the above method:

**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 predicate)

**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 Geeks { // Method to check if the specified element // is present in the array or not private static boolean isElementPresent(int[] arr, int key) { // check if the specified element // is present in the array or not // using anyMatch() method boolean res = IntStream.of(arr) .anyMatch(x -> x == key);

    return res;
}

public static void main(String[] args)
{

    // Get the array
    int arr[] = { 3, 5, 7, 2, 6, 10 };

    // Get the value to be checked
    int key = 7;

    // Check if this value is
    // present in the array or not
    boolean res = isElementPresent(arr, key);

    System.out.println("Is " + key
                       + " present in the array: "
                       + res);
    
}

}

`

Output

Is 7 present in the array: true

**Complexity Analysis:

**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 Geeks { // Method to check if the specified element // is present in the array or not private static boolean isElementPresent(int[] arr, int key) { // check if the specified element // is present in the array or not // using anyMatch() method boolean test = Arrays.stream(arr) .anyMatch(x -> x == key);

    return test;  
}

public static void main(String[] args)
{
    // Get the array
    int arr[] = { 3, 5, 7, 2, 6, 10 };

    // Get the value to be checked
    int key = 7;

    // Check if this value is
    // present in the array or not
    boolean res = isElementPresent(arr, key);

    System.out.println("Is " + key
                     + " present in the array: "
                     + res);
}

}

`

Output

Is 7 present in the array: true

**Complexity Analysis: