Java program to search an element in an array
In this post, we will write a Java program to search for an element in an array. There are many searching algorithms available to search for an element in an array. Here, we have mentioned different approaches to achieve this task-
Java program to search an element in an array using Linear Search
A linear search is a method for finding an element within a list or an array. It sequentially checks each element of the array until a match is found or the whole array has been searched.
import java.util.Arrays;
import java.util.stream.IntStream;
public class Search_Element {
private static void checkElement(int[] arr, int key)
{
// check if the specified element
// is present in the array
boolean flag = false;
for (int element : arr) {
if (element == key) {
flag = true;
break;
}
}
// Print the result
System.out.println("Element found.");
}
public static void main(String[] args)
{
// Initialize array
int arr[] = { 78, 42, 89, 11, 56, 10 };
// Get the value to be checked
int key = 11;
// Print the array
System.out.println("Array: "
+ Arrays.toString(arr));
checkElement(arr, key);
}
}
Output of the above code:
Array: [78, 42, 89, 11, 56, 10]
Element found.
Java program to search an element in an array using Binary Search
Binary search is an efficient algorithm for finding an item from a sorted list of items.
import java.util.Arrays;
import java.util.stream.IntStream;
public class BinarySearch {
// Function return true if given element
// found in array
private static void checkElement(int[] arr, int key)
{
// sort given array
Arrays.sort(arr);
int res = Arrays.binarySearch(arr, key);
boolean flag = res > 0 ? true : false;
// Print the result
if(flag) {
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}
public static void main(String[] args)
{
// Initialize array
int arr[] = {98, 34, 64, 22, 90, 29, 70, 62 };
// Get the value to be checked
int element = 90;
// Print the array
System.out.println("Array: "
+ Arrays.toString(arr));
checkElement(arr, element);
}
}
Output of the above code:
Array: [98, 34, 64, 22, 90, 29, 70, 62]
Element found.
Java program to search an element in an array using List.contains() Method
The contains() method of the List interface in Java is used to check if the specified element exists in the given list/array or not. This method accepts a single parameter whose presence in this list is to be tested. It returns true if the specified element is found in the list, else it returns false.
import java.util.Arrays;
public class SearchElementJava {
private static void checkElement(Integer[] arr, int key)
{
boolean flag
= Arrays.asList(arr)
.contains(key);
// Print the result
if(flag) {
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}
public static void main(String[] args)
{
// Initialize array
Integer arr[] = { 90, 44, 78, 27, 54, 92, 80, 72 };
// Get the value to be checked
int key = 80;
// Print the array
System.out.println("Array: "
+ Arrays.toString(arr));
checkElement(arr, key);
}
}
Output of the above code:
Array: [90, 44, 78, 27, 54, 92, 80, 72]
Element found.
Java program to search an element in an array using Stream.anyMatch() Method
The Stream.anyMatch() method returns whether any elements of this stream match the provided predicate. It returns true if at least one element satisfies the condition provided by the predicate, else false.
import java.util.Arrays;
import java.util.stream.IntStream;
public class SearchElement {
// Function return true if given element
// found in array
private static void checkElement(int[] arr, int key)
{
boolean flag
= IntStream.of(arr)
.anyMatch(x -> x == key);
// Print the result
if(flag){
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}
public static void main(String[] args)
{
// Initialize array
int arr[] = { 90, 53, 34, 89, 20, 78, 52 };
// Get the value to be checked
int element = 78;
// Print the array
System.out.println("Array: "
+ Arrays.toString(arr));
checkElement(arr, element);
}
}
Output of the above code:
Array: [90, 53, 34, 89, 20, 78, 52]
Element found.
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java