
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If an Array Contains a Given Value in Java
In this article, we will understand how to check if an array contains a given value using two methods: Linear search and the HashSet class. For Linear Search, we will iterate through the array elements, comparing each element with the given input. For the HashSet class, we will utilize the contains() method to achieve this efficiently.
Problem Statement
Write a Java program to check if an array contains the given value ?
Input 1
Enter the number to be searched: 25 The elements in the integer array: 15 20 25 30 35
Output 1
The array contains the given valueInput 2
languages = Hindi, English, French, JapaneseOutput 2
English
English is in the array
Different Approaches
Following are the different approaches to check if an array contains the given value ?
Using Linear search approach
Below are the steps to check if an array contains the given value the input is entered by the user ?
- Import Scanner class.
- Declare variables: my_input , i, array_size, and my_check (boolean).
- Initialize Scanner object my_scanner.
- Prompt and read my_input and array_size.
- Declare integer array my_array of size array_size.
- Iterate through my_array with a for loop to check if any element equals my_input.
- If a match is found, set my_check to true and break the loop.
- Print whether the array contains the given value based on my_check.
Example
Here, is an example in which the input is entered by the user?
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
Using HashSet class
Below are the steps to check if an array contains the given value using HashSet class ?
- First import the HashSet class from the java.util package.
- Create a string array and initialize it with language values.
- Create a HashSet object to store unique languages.
- Iterate through the array and add each language to the HashSet.
- Define a target language to search for in the array.
- Use the contains() method of the HashSet to check if it contains the target language.
Example
Here is an example to check if an array contains the given value using HashSet class ?
import java.util.HashSet; public class Main { public static void main(String[] args) { String[] languages = {"Hindi", "English", "French", "Japanese"}; String targetlanguage = "English"; HashSet<String>languageSet = new HashSet<>(); for (String language : languages) { languageSet.add(language); } if (languageSet.contains(targetlanguage)) { System.out.println(targetlanguage + " is in the array"); } else { System.out.println(targetlanguage + " is not in the array"); } } }
Output
English is in the array