Java Program to Check if An Array Contains a Given Value



Following is a Java program to check if An Array Contains a Given Value.

Example

Live Demo

public class Sample {
   public static void main(String args[]){
      int[] myArray = {55, 45, 69, 44};
      int num = 55;

      for(int i = 0; i<myArray.length; i++){
         if(num == myArray[i]){
            System.out.println("Array contains the given element");
         }
      }
   }
}

Output

Array contains the given element

Advertisements