In this article, we will understand how to check if an array contains the given value. This is accomplished by iterating upon the array elements and comparing the given input with the array elements.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the number to be searched: 25 The elements in the integer array: 15 20 25 30 35
Output
The desired output would be −
The array contains the given value
Algorithm
Step 1 - START Step 2 - Declare three integer values namely my_input , i, array_size. A Boolean value my_check is defined and an integer array my_array is defined Step 3 - Read the required values from the user/ define the values Step 4 - Iterate the elements using a for loop and compare the values of the given input with in array values. Step 5 - If the values match, the element is present. If not, the element is not present. Step 6 - Display the result Step 7 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class Main { public static void main(String[] args) { int my_input , i, array_size; boolean my_check = false; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.println("Enter the number to be searched "); my_input = my_scanner.nextInt(); System.out.print("Enter the value of array_size : "); array_size = my_scanner.nextInt(); int my_array[] = new int[array_size]; System.out.println("Enter the elements of the array :" ); for ( i = 0 ; i < array_size ; i++ ){ my_array[i] = my_scanner.nextInt(); } for ( i = 0 ; i < array_size ; i++ ) { if (my_array[i] == my_input) { my_check = true; break; } } if(my_check) System.out.println("\nThe array contains the given value"); else System.out.println("\nThe array doesnot contain the given value"); } }
Output
Required packages have been imported A reader object has been defined Enter the number to be searched 25 Enter the size of array : 5 Enter the elements of the array : 10 15 20 25 30 The array contains the given value
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Main { public static void main(String[] args) { int[] my_array = {15, 20, 25, 30, 35, 40}; int my_input , i, array_size; array_size = 5; my_input = 25; boolean my_check = false; System.out.println("The number is defined as " +my_input); System.out.println("The elements in the integer array is defined as :" ); for ( i = 0 ; i < array_size ; i++ ){ System.out.print(my_array[i] +" "); } for ( i = 0 ; i < array_size ; i++ ) { if (my_array[i] == my_input) { my_check = true; break; } } if(my_check) System.out.println("\nThe array contains the given value"); else System.out.println("\nThe array doesnot contain the given value"); } }
Output
The number is defined as 25 The elements in the integer array is defined as : 15 20 25 30 35 The array contains the given value