Checking if array contains given element

check if value exists in array javascript
check if value already exists in array javascript
check if array contains value java
javascript check if value exists in array of objects
angular 4 check if array contains value
javascript array contains object
array includes javascript
check if an array contains any elements in another array in java

I have an array of numbers and dynamically adding new numbers to that array in for loop. But I need to avoid adding values that already exist in array. Is there a JS native way to check the presence of some value in array without doing 2nd nested loop. I don't want to use nested loop because the size of array may vary up to 10000

You can easily avoid a for loop by using a while loop. [Pause for laughter...] But seriously, even the built-in Array.indexOf() method (supported by most browsers) probably uses a loop internally.

You could use a plain object instead, and add each number to the object as a property, then afterwards take the values from the object and put them in an actual array (or just use them in the object if that is convenient). Then you only have to loop through the "up to 10000" numbers once at the end:

var numbersObj = {},
    numbersArray = [];

// your existing for statement here
for (var i=0; i<something; i++) {
   var currentNumber = somethingElse(); // whatever your existing code is to
                                    // determine the number to add goes here

   // put the number in the object (as a key)
   numersObj[currentNumber] = true;

// copy numbers out of object into array
for (var k in numbersObj)
   if (numbersObj.hasOwnProperty(k))

After which numbersArray contains unique numbers only. The if test with .hasOwnProperty() is "optional" depending on your point of view.

Within the first loop you could check if numbersObj already holds the currentNumber:

if (!numbersObj[currentNumber])
   numbersObj[currentNumber] = true;

Or just (over)write it every time like I did in the first code block.

Check if an element is present in an array, ECMAScript 2016 incorporates an includes() method for arrays that specifically solves the problem, and so is now the preferred method. [1, 2, 3].includes(2);  Write a function to check if the array contains all elements in the given range. Examples : The most intuitive approach is to sort the array and check from the element greater than ‘A’ to the element greater than ‘B’. If these elements are in continuous order, all elements in the range exists in the array.

You can use JavaScript's native Array.prototype.indexOf, supported in most browsers:

 var a = [1, 2, 3];
 console.log(a.indexOf(4));   // -1

indexOf is faster than a for-loop but the algorithmic complexity is still O(n^2). If the size of the array gets much larger, consider a different data structure such as a hash table.

How to Check If a Value Exists in an Array in JavaScript, You can use the indexOf() method to check whether a given value or element exists in an array or not. The indexOf() method returns the index of the element  In the above program, we have an array of integers stored in variable num. Likewise, the number to be found is stored in toFind. Now, we use a foreach loop to iterate through all elements of num and check individually if toFind is equal to n or not. If yes, we set found to true and break from the loop.

Just use includes.

var array1 = [1, 2, 3];
console.log(array1.includes(2)); // true

Check if an array contains all elements of a given 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 : 5 Output : Yes Input :  January 16, 2020 No Comments algorithms, c / c++, java, javascript, python Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1:

try this,

function eleContainsInArray(arr,element){
    if(arr != null && arr.length >0){
        for(var i=0;i<arr.length;i++){
            if(arr[i] == element)
                return true;
    return false;

Check if a value is present in an Array in Java, contains() Method: List contains() method in Java is used for checking if the specified element exists in the given list or not. Syntax: public boolean contains(​Object)  Array.prototype.indexOf-> (returns index or -1) is generally used for finding index of element in array. This can also be used for searching object but only works if you are passing reference to same object.

There is Array.indexOf which is supported from some browser, you can use this snippets of code to support all browser

if (!Array.prototype.indexOf)
  Array.prototype.indexOf = function(elt /*, from*/)
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
      if (from in this &&
          this[from] === elt)
        return from;
    return -1;

How to Check if an Array Contains a Value in Java Efficiently?, How to check if an array (unsorted) contains a certain value? This is a very useful and frequently used operation in Java. It is also a top voted question on. Definition and Usage. The includes() method determines whether an array contains a specified element. This method returns true if the array contains the element, and false if not. Note: The includes() method is case sensitive.

Java Program to Check if An Array Contains a Given Value, If any of the element n returns true , found is set to true as well. Example 3: Check if array contains a given value for non-primitive types. import java. Check if all elements of the given array can be made 0 by decrementing value in pairs Check if URL is valid or not in Java Check if the array has an element which is equal to product of remaining elements

Java – Check if Array contains a certain value? –, Java examples to check if an Array (String or Primitive type) contains a certain values, updated with Java 8 stream APIs. In the above program, we've used a non-primitive data type String and used Arrays's stream() method to first convert it to a stream and anyMatch() to check if array contains the given value toFind. Here's the equivalent Java code: Java program to check if array contains a given value.

How to check if array includes a value in JavaScript?, Here's a Code Recipe to check if a #JavaScript array contains a value. can use the in operator to loop over an object to check if a specific property key exists. Array.Exists(T[], Predicate<T>) Method is used to check whether the specified array contains elements that match the conditions defined by the specified predicate. Syntax: public static bool Exists<T> (T[] array, Predicate<T> match);

  • Possible duplicate:…
  • thanks for your comment :) yes it is...
  • great solution :) I really like it
  • in this case I even don't need the last for to convert the object to array, because in my case there is no difference where the final values are stored :)
  • now in 2017 when we have Set support in JS my question seems so funny :)
  • thanks for your solution, but the other solution fits to my needs more, so I'm checking that one as the answer
  • Sounds good. JS objects are just hash maps, which @nnnnnn explicitly showed you how to use. :)
  • this again uses for loop, which I need to avoid
  • this uses for loop, I don't want use loops
  • @haynar, is this one of these "write a program without using parentheses" type of questions?
  • I dont want use explicit loop, I want to use native method because it's more efficient than custom for loop (I know that internally it uses loop)
  • @haynar this uses a loppo only if there is no explicit method defined in the browser