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
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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
private static void check( int [] arr, int toCheckValue)
{
boolean test = false ;
for ( int element : arr) {
if (element == toCheckValue) {
test = true ;
break ;
}
}
System.out.println( "Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 1 , 9 , 7 , 2 , 6 , 10 };
int toCheckValue = 7 ;
System.out.println( "Array: "
+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}
|
OutputArray: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
Time Complexity: O(N)
Auxiliary Space: O(1)
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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
private static void check( int [] arr, int toCheckValue)
{
Arrays.sort(arr);
int res = Arrays.binarySearch(arr, toCheckValue);
boolean test = res >= 0 ? true : false ;
System.out.println( "Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 1 , 9 , 7 , 2 , 6 , 10 };
int toCheckValue = 7 ;
System.out.println( "Array: "
+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}
|
OutputArray: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
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
import java.util.Arrays;
class GFG {
private static void check(Integer[] arr, int toCheckValue)
{
boolean test
= Arrays.asList(arr)
.contains(toCheckValue);
System.out.println( "Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args)
{
Integer arr[] = { 5 , 1 , 1 , 9 , 7 , 2 , 6 , 10 };
int toCheckValue = 7 ;
System.out.println( "Array: "
+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}
|
OutputArray: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
Time Complexity: O(N)
Auxiliary Space: O(1)
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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
private static void check( int [] arr, int toCheckValue)
{
boolean test
= IntStream.of(arr)
.anyMatch(x -> x == toCheckValue);
System.out.println( "Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 1 , 9 , 7 , 2 , 6 , 10 };
int toCheckValue = 7 ;
System.out.println( "Array: "
+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}
|
OutputArray: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
Time Complexity: O(N)
Auxiliary Space: O(1)
Example 2: Using Arrays.stream() method to create Stream
Java
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
private static void check( int [] arr, int toCheckValue)
{
boolean test
= IntStream.of(arr)
.anyMatch(x -> x == toCheckValue);
System.out.println( "Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 1 , 9 , 7 , 2 , 6 , 10 };
int toCheckValue = 7 ;
System.out.println( "Array: "
+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}
|
OutputArray: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
Time Complexity: O(N)
Auxiliary Space: O(1)