Check If Two Arrays Have Same Elements In JavaScript

To check if two arrays have same elements in JavaScript, you can use the following methods:

1. Use the every() method and length property if you care about the order of elements in the two arrays.

2. Use the includes() method and length property if you do not care about the order of elements in the two arrays.

Let’s look at each of these methods in detail below.

Check If Two Arrays Have Same Elements In JavaScript

check if two arrays have same elements in javascript

1. Use The every() Method And Length Property

The every() method is used to check if all elements in an array pass a test provided as a function.

For our purposes, we can use it to check if all elements in one array are present in another array.

We can also use the length property to make sure that both arrays have the same number of elements.

Example:

arr1 = ["a", "b", "c"];

arr2 = ["a", "b", "c"];

function checkIfSame(arr1, arr2) {

   return arr1.length === arr2.length && arr1.every((element, index) => element === arr2[index]);

}

console.log(checkIfSame(arr1, arr2)); // true

In the example above, we first check if the length of both arrays is the same using the === operator.

We then use the every() method to check if each element in arr1 is present in arr2 at the same index.

If both these conditions are true, then we return true from the function, indicating that both arrays have the same elements.

This method will not return true if elements are present at different indices in the two arrays.

Example:

arr1 = ["a", "b", "c"];

arr2 = ["c", "a", "b"];

function checkIfSame(arr1, arr2) {

   return arr1.length === arr2.length && arr1.every((element, index) => element === arr2[index]);

}

console.log(checkIfSame(arr1, arr2)); // false

As you can see from the example above, even though both arrays have the same elements, the every() method will not return true because the order of elements is different in the two arrays.

2. Use The includes() Method And Length Property

Another way to check if two arrays have same elements in JavaScript is to use the includes() method. You can use this method if you don’t care about the order of the elements.

The includes() method is used to check if an array contains a particular element.

We can use it to check if all elements in one array are present in another array.

Like before, we can also use the length property to make sure that both arrays have the same number of elements.

Example:

arr1 = ["a", "b", "c"];

arr2 = ["c", "a", "b"];

function checkIfSame(arr1, arr2) {

  return arr1.length === arr2.length && arr1.every(element => arr2.includes(element));

}

console.log(checkIfSame(arr1, arr2)); // true

In the example above, we first check if the length of both arrays is the same using the === operator.

We then use the every() method to check if each element in arr1 is present in arr2.

If both these conditions are true, then we return true from the function, indicating that both arrays have the same elements.

Conclusion

In this article, you learned two ways to check if two arrays have same elements in JavaScript.

You can use every() method or the every() and includes() method in combination with the length property to achieve this.

I hope you found this article helpful.

Happy coding!

Leave a Reply