Scala program to check if an array contains a specific value

1. Introduction

One common operation when working with arrays is to determine if an array contains a specific value. This can be useful in numerous scenarios such as filtering data, checking prerequisites, or validating inputs. Scala provides an easy and intuitive way to perform this check using the contains method. Let's delve into how to implement this.

2. Program Steps

1. Set up the Scala environment.

2. Create an array with sample values.

3. Specify a value to search for within the array.

4. Use the contains method to determine if the array has the specified value.

5. Display the result.

6. Run the program.

3. Code Program

object ArrayContainsApp {
  def main(args: Array[String]): Unit = {
    // Sample array
    val numbers = Array(10, 20, 30, 40, 50)

    // Value to search for
    val searchValue = 30

    // Check if the array contains the search value
    val containsValue = arrayContains(numbers, searchValue)

    println(s"The array ${numbers.mkString("[", ", ", "]")} ${if (containsValue) "contains" else "does not contain"} the value $searchValue.")

  def arrayContains(arr: Array[Int], value: Int): Boolean = {


The array [10, 20, 30, 40, 50] contains the value 30.


1. We declare an ArrayContainsApp object to encapsulate our program logic.

2. Within the main function, we set up a sample array called numbers.

3. We specify a searchValue that we want to determine if it exists within the numbers array.

4. The arrayContains function is utilized to determine the presence of the searchValue in the array. This function leverages Scala's contains method available for arrays to perform the check.

5. The result is then printed out, indicating whether or not the searchValue is present within the numbers array.