Java Program to Check if Array Contains a Given Value - The Coding Shala

Home >> Java Programs >> Check if Array Contains a Given Value

 Hey there, welcome back to another post. In this post, we will learn how to check if the array contains a given value or not in Java.

Java Program to Check if Array Contains a Given Value

Write a Java program to check if an array contains a given value or not. 

Example 1: 

Input Array: [1, 5, 7, 3, 9]
Given value: 7
Output:
7 is present in the array

We can check this using the below ways: 

  1. Using a loop.
  2. using List.contains() method.

Let's see the above methods using Java Examples.

Java Example to Check if Array Contains a Given Value using For Loop

We can use a for loop to iterate the array and will check if the given value is available or not in the array.

Java Program: 

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static void main(String[] args) {
        // integer array
        int[] arr = {4, 3, 2, 11, 90, -7, 45, 2};
        System.out.println("The array is: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();

        // using for loop
        int check = 11;
        int flag = 0;
        for (int num : arr) {
            if (num == check) {
                flag = 1;
                break;
            }
        }

        if (flag == 1) {
            System.out.println(check + " is present in the above array");
        } else {
            System.out.println(check + " is not present in the above array");
        }
    }
}

Output: 

The array is: 
4 3 2 11 90 -7 45 2 
11 is present in the above array

Java Example to Check if Array Contains a Given Value using contains() method

We can use the List's contains() method to check if the given value is available or not in the array. On array directly we can't use contains method so first convert array to List then use it.

Java Program: 

import java.util.Arrays;
import java.util.List;

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static void main(String[] args) {
        // integer array
        int[] arr = {4, 3, 2, 11, 90, -7, 45, 2};
        System.out.println("The array is: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();

        // using List.contains() method
        int check = 10;
        List list = Arrays.asList(arr);
        if (list.contains(check)) {
            System.out.println(check + " is present in the above array.");
        } else {
            System.out.println(check + " is not present in the above array.");
        }
    }
}

Output: 

The array is: 
4 3 2 11 90 -7 45 2 
10 is not present in the above array.


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala