How to check if an Array contains a value or not? Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-built function for searching C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v{ 10, 25, 15, 12, 14 }; int key = 15; // Using inbuilt function 'find' if (find(v.begin(), v.end(), key) != v.end()) cout << key << " is present in the array"; else cout << key << " is not present in the array"; return 0; } Java /*package whatever // do not write package name here */ import java.io.*; import java.util.Arrays; class GFG { public static void main(String[] args) { Integer arr[] = { 10, 25, 15, 12, 14 }; int key = 15; // Using inbuilt function 'contains' boolean found = Arrays.asList(arr).contains(key); if (found == true) { System.out.println( key + " is present in the array"); } else { System.out.println( key + " is not present in the array"); } } } Python3 if __name__ == '__main__': arr = [10, 25, 15, 12, 14] key = 15 found = False if key in arr: found = True if found == True: print(key, end = " is present in the array") else: print(key, end = " is not present in the array") C# // C# Code for above approach using System; public class GFG { public static void Main(string[] args) { int []arr = { 10, 25, 15, 12, 14 }; int key = 15; // Using inbuilt function 'contains' bool found = Array.Exists(arr, x => x == key); if (found == true) { Console.WriteLine(key + " is present in the array"); } else { Console.WriteLine(key + " is not present in the array"); } } } // This code is contributed by AnkThon PHP <?php $arr = array(10, 25, 15, 12, 14); $key = 15; if (in_array("$key", $arr)){ echo "$key is present in the array"; } else{ echo "$key is not present in the array"; } ?> JavaScript <script> const arr = [10, 25, 15, 12, 14]; const key = 15 if(arr.includes(key) == true){ console.log( key + " is present in the array"); } else{ console.log( key + " is not present in the array"); } </script> Output15 is present in the array Time Complexity: O(N) Auxiliary Space: O(1) Apart from these inbuilt functions, there are other methods that one can use like: Linear searchBinary searchTernary search, andOther searching algorithms Comment More infoAdvertise with us Next Article How to check if an Array contains a value or not? P prince_patel Follow Improve Article Tags : Data Structures DSA Practice Tags : Data Structures Similar Reads How to check if a value exists in an Array in Ruby? In this article, we will discuss how to check if a value exists in an array in ruby. We can check if a value exists in an array through different methods ranging from using Array#member? method and Array#include? method to Array#any? method Table of Content Check if a value exists in an array using 3 min read How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript 2 min read How to check an Array Contains an Object of Attribute with Given Value in JavaScript ? Sometimes, when working with an array of objects in JavaScript, we need to determine whether an object with a specific attribute value exists in the array. This can be useful when filtering or searching for specific objects in the array. Below are the common methods to determine if the array contain 4 min read How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP 2 min read How to Check a Value Exist at Certain Array Index in JavaScript ? Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index 5 min read Like