Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Data Structures
  • Array
  • String
  • Linked List
  • Stack
  • Queue
  • Tree
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Graph
  • Trie
  • Segment Tree
  • Disjoint Set Union
  • Fenwick Tree
  • AVL Tree
  • Red-Black Tree
  • Advanced Data Structures
Open In App
Next Article:
How to check if a value exists in an Array in Ruby?
Next article icon

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 = 17
Output: True

Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20
Output: 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> 

Output
15 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 search
  • Binary search
  • Ternary search, and
  • Other searching algorithms

Next Article
How to check if a value exists in an Array in Ruby?

P

prince_patel
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
  • Check If An Array of Strings Contains a Substring in JavaScript
    Here are the different ways to check if an array of strings contains a substring in JavaScript 1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string. [GFGTABS] JavaScript const a =
    4 min read
  • Check if an array contains all elements of a given range
    An array containing positive elements is given. 'A' and 'B' are two numbers defining a range. Write a function to check if the array contains all elements in the given range. Examples : Input : arr[] = {1 4 5 2 7 8 3} A : 2, B : 5Output : YesInput : arr[] = {1 4 5 2 7 8 3} A : 2, B : 6Output : NoRec
    15+ 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 inde
    5 min read
  • How to check whether an array includes a particular value or not in JavaScript ?
    In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The followin
    3 min read
  • Check if an Array is Empty or not in TypeScript
    In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
    2 min read
  • How Check if object value exists not add a new object to array using JavaScript ?
    In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = {
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences