Java Program to Check if An Array Contains a Given Value

Converting number to string in c++

What is  an Array?

In Java, an array is a data structure that stores a fixed-size sequential collection of elements of the same data type. Arrays are useful for storing collections of data that need to be accessed quickly and efficiently, such as lists of numbers or strings.

 

Algorithm to check if an array contains a given value or not :

  1. Iterate through the elements of the array.
  2. For each element, check if it is equal to the value you are searching for.
  3. If the element is equal to the value, return true.
  4. If the end of the array is reached and the value has not been found, return false.

Pseudo code that implements this algorithm:

public boolean containsValue(int[] array, int value) {
  for (int element : array) {
    if (element == value) {
      return true;
    }
  }
  return false;
}

Example :

Run
public class Main {

  public static void main(String[] args) {
    int[] numbers = {1, 2, 3, 4, 5};
    boolean containsValue = containsValue(numbers, 3);
    if (containsValue) {
      System.out.println("The array contains the value.");
    } else {
      System.out.println("The array does not contain the value.");
    }
  }

  public static boolean containsValue(int[] array, int value) {
    for (int element : array) {
      if (element == value) {
        return true;
      }
    }
    return false;
  }
}

Output :

The array contains the value.
Explanation : 

This program declares and initializes an array of integers called numbers, and then calls the containsValue method, passing it the numbers array and the value 3.

The containsValue method iterates through the elements of the array and checks if any of them are equal to the given value. If it finds a match, it returns true. If it reaches the end of the array and no match is found, it returns false.

The value returned by the containsValue method is then used to print a message to the console indicating whether or not the value was found in the array.

Example :

Run
public class Main {

  public static void main(String[] args) {
    int[] numbers = {1, 2, 3, 4, 5};
    int value = 3;
    boolean containsValue = containsValue(numbers, value);
    if (containsValue) {
      System.out.println("The array contains the value " + value + ".");
    } else {
      System.out.println("The array does not contain the value " + value + ".");
    }
  }

  public static boolean containsValue(int[] array, int value) {
    for (int element : array) {
      if (element == value) {
        return true;
      }
    }
    return false;
  }
}

Output :

The array contains the value 3.
 
Explanation :

This program declares an array of integers called numbers and a variable called value, and then calls the containsValue method, passing it the numbers array and the value variable.

The containsValue method iterates through the elements of the array and checks if any of them are equal to the given value. If it finds a match,

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription