

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between first and the second array in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. The arrays might contain some identical entries as well.
The purpose of our function is to simply find out and return an array of all such elements that exists in the first array but not in the second.
Example
The code for this will be −
const arr1 = ['1', '2', '3', '4/2', '5/4', '6−2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6−1', '7/2', '8−2']; const differenceBetween = (arr1 = [], arr2 = []) => { const res = []; for(let i = 0; i < arr1.length; i++){ const el = arr1[i]; if(arr2.includes(el)){ continue; }; res.push(el); }; return res; }; console.log(differenceBetween(arr1, arr2));
Output
And the output in the console will be −
['6−2']
- Related Questions & Answers
- Maximize first array over second in JavaScript
- Difference Between First level cache and Second level cache in Hibernate
- Sort the second array according to the elements of the first array in JavaScript
- Calculate the difference between the first and second element of each subarray separately and return the sum of their differences in JavaScript
- Find the first, second and third minimum elements in an array in C++
- Product of maximum in first array and minimum in second in C
- Find the first, second and third minimum elements in an array in C++ program
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Elements present in first array and not in second using STL in C++
- Difference Between Array and Structure
- Difference Between Array and Pointer
- Difference between sum and product of an array in JavaScript
- Maximum difference between first and last indexes of an element in array in C
- What is the difference between == and === in JavaScript?
- Find the second most frequent element in array JavaScript
Advertisements