- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if values of two arrays are the same/equal in JavaScript
We have two arrays of numbers, let’s say −
[2, 4, 6, 7, 1] [4, 1, 7, 6, 2]
Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order.
For example −
[2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently.
Now, let’s write the code for this function −
Example
const first = [2, 4, 6, 7, 1]; const second = [4, 1, 7, 6, 2]; const areEqual = (first, second) => { if(first.length !== second.length){ return false; }; for(let i = 0; i < first.length; i++){ if(!second.includes(first[i])){ return false; }; }; return true; }; console.log(areEqual(first, second));
Output
The output in the console will be −
true
- Related Articles
- How to know if two arrays have the same values in JavaScript?
- Return True if all entries of two arrays are equal in Numpy
- Check if some elements of array are equal JavaScript
- Check if two SortedSet objects are equal in C#
- Check if two BitArray objects are equal in C#
- Check if two ArrayList objects are equal in C#
- Check if two HashSet objects are equal in C#
- Check if two HybridDictionary objects are equal in C#
- Check if two LinkedList objects are equal in C#
- Check if two StringBuilder objects are Equal in C#
- Check if two Tuple Objects are equal in C#
- Check if two StringCollection objects are equal in C#
- Check if two OrderedDictionary objects are equal in C#
- Check if two SortedList objects are equal in C#
- Check if two List objects are equal in C#

Advertisements