- 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
Compare two arrays of single characters and return the difference? JavaScript
We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array.
Example of two such arrays are −
const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T'];
We will check each character at the same position and return only the parts who are different.
Example
const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; const findDifference = (arr1, arr2) => { const min = Math.min(arr1.length, arr2.length); let i = 0; const res = []; while (i < min) { if (arr1[i] !== arr2[i]) { res.push(arr1[i], arr2[i]); }; ++i; }; return res.concat(arr1.slice(min), arr2.slice(min)); }; console.log(findDifference(arr1, arr2));
Output
And the output in the console will be −
[ 'A', 'F', 'C', 'A', 'A', 'T', 'D', 'T' ]
- Related Articles
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays and return the element-wise maximum in Numpy
- Compare two Numpy arrays and return the element-wise maximum with fmax()
- Compare two Numpy arrays and return the element-wise minimum ignoring NaNs
- Compare two Numpy arrays and return the element-wise minimum with fmin()
- Compare two arrays and return the element-wise maximum ignoring NaNs in Numpy
- Compare two int arrays in a single line in Java
- Compare two short arrays in a single line in Java
- Compare two double arrays in a single line in Java
- Compare two long arrays in a single line in Java
- Compare two float arrays in a single line in Java
- Compare two char arrays in a single line in Java
- Compare two-byte arrays in a single line in Java
- Compare and return True if two string Numpy arrays are not equal
- Compare and return True if two string arrays are equal in Numpy

Advertisements