If array not contains javascript

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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;

        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

    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;

            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#

    using System;

    public class GFG {

        public static void Main(string[] args)

        {

            int []arr = { 10, 25, 15, 12, 14 };

            int key = 15;

            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");

            }

        }

    }

    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

    How do you know if something is not in an array?

    The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.

    Does not exist in array JavaScript?

    To push an element to an array if it doesn't exist, use the indexOf() method to check if the value is present in the array. The indexOf method returns -1 if the value is not contained in the array, in which case we should use the push() method to add it.

    How do you check if an array contains an object JavaScript?

    Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false.

    How do I check if an array is empty in JavaScript?

    The array can be checked if it is empty by using the array. length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.