Check if array contains value java: In the previous article, we have seen Java Program to Find Cumulative Sum of an Array
In this article we are going to check if a specific value is present in array or not using Java.
Java Program to Check if an Array Contains a Specific Value
How to see if an array contains a value in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find an element present in array.
Method-1: Java Program to Check if an Array Contains a Specific Value By Using Linear Approach
Approach:
- Create an array of elements.
- Display the array.
- Ask the user to enter a value which we want to check that it is present in the array or not.
- Traverse through the array to find elements matching.
- If the searched element is found in the array then that array contains that specific value.
- If the searched element is not found in the array then that array does not contain that specific value.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Crating an array int arr[] = { 9, 5, 7, 8, 6, 3}; System.out.print("Array: "); printArray(arr); // Taking item input System.out.println("Enter item to check if it is present or not: "); Scanner scan = new Scanner(System.in); int item = scan.nextInt(); // Traversinng the array looking for the element int index = -1; for(int i = 0; i<arr.length; i++) { if(arr[i]==item) { index = i; break; } } // Printing the final result if(index == -1) System.out.println("Entered element is not present in the array."); else System.out.println("Entered element is present in the array."); } // Function to print the array static void printArray(int arr[]) { for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } }
Output: Case-1 Array: 9 5 7 8 6 3 Enter item to check if it is present or not: 6 Entered element is present in the array. Case-2 Array: 9 5 7 8 6 3 Enter item to check if it is present or not: 23 Entered element is not present in the array.
Method-2: Java Program to Check if an Array Contains a Specific Value By Using Binary Approach
Approach:
- Create an array.
- Sort the array.
- Implement Binary Search algorithm to check if the searched element is present or not.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Crating an array int arr[] = { 9, 5, 7, 8, 6, 3}; // Sorting the array Arrays.sort(arr); System.out.print("Array: "); printArray(arr); // Taking item input System.out.println("Enter item to search"); Scanner scan = new Scanner(System.in); int item = scan.nextInt(); // Calling Binarysearch function int index = binSearch(arr,0,arr.length,item); // Printing the final result if(index == -1) System.out.println("Item is not present in the array"); else System.out.println("Item is present in the array"); } // Function to print the array static void printArray(int arr[]) { for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } // Binary Search Algorithm // Checks whether the element is present inside the sorted array static int binSearch(int arr[], int l, int r, int item) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == item) return mid; if (arr[mid] > item) return binSearch(arr, l, mid - 1, item); return binSearch(arr, mid + 1, r, item); } return -1; } }
Output: Array: 3 5 6 7 8 9 Enter item to search 6 Item is present in the array
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: