Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if array has element(s) from another array

I have two arrays A = [0,1,2] and B = [2,1,0]. How to check if a number in A is present in B?

like image 534
Vikram S Avatar asked Mar 11 '18 09:03

Vikram S


People also ask

How do you check if an array contains an element from another array?

Javascript array contains another array To check if the array contains an array in Javascript, use array some(), and array includes() function. The array some() method checks each element against a test method and returns true if any array item passes the test function.

How do you find an array within an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

How do you get the elements of one array which are not present in another array using JavaScript?

Use the . filter() method on the first array and check if the elements of first array are not present in the second array, Include those elements in the output.


3 Answers

NOTE: includes is not ES6, but ES2016 Mozilla docs. This will break if you transpile ES6 only.

You can use Array#every method(to iterate and check all element passes the callback function) with Array#includes method(to check number present in B).

A.every( e => B.includes(e) )

const A = [0, 1, 2],
  B = [2, 1, 0],
  C=[2, 1];

console.log(A.every(e => B.includes(e)));
console.log(A.every(e => C.includes(e)));
console.log(C.every(e => B.includes(e)));

To check a single element present in the second array do:
A[0].includes(e) 
//^---index

Or using Array#indexOf method, for older browser.

A[0].indexOf(e) > -1 

Or in case you want to check at least one element present in the second array then you need to use Array#some method(to iterate and check at least one element passes the callback function).

A.some(e => B.includes(e) )

const A = [0, 1, 2],
  B = [2, 1, 0],
  C=[2, 1],D=[4];

console.log(A.some(e => B.includes(e)));
console.log(A.some(e => C.includes(e)));
console.log(C.some(e => B.includes(e)));
console.log(C.some(e => D.includes(e)));
like image 130
Pranav C Balan Avatar answered Oct 10 '22 10:10

Pranav C Balan


Here's a self defined function I use to compare two arrays. Returns true if array elements are similar and false if different. Note: Does not return true if arrays are equal (array.len && array.val) if duplicate elements exist.

var first = [1,2,3];
var second = [1,2,3];
var third = [3,2,1];
var fourth = [1,3];
var fifth = [0,1,2,3,4];

console.log(compareArrays(first, second));
console.log(compareArrays(first, third));
console.log(compareArrays(first, fourth));
console.log(compareArrays(first, fifth));

function compareArrays(first, second){
    //write type error
    return first.every((e)=> second.includes(e)) && second.every((e)=> first.includes(e));
}
like image 4
Kiren James Avatar answered Oct 10 '22 08:10

Kiren James


If the intent is to actually compare the array, the following will also account for duplicates

const arrEq = (a, b) => {
  if (a.length !== b.length) {
    return false
  }
  const aSorted = a.sort()
  const bSorted = b.sort()


  return aSorted
    .map((val, i) => bSorted[i] === val)
    .every(isSame => isSame)
}

Hope this helps someone :D

like image 2
Right2Drive Avatar answered Oct 10 '22 08:10

Right2Drive