
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Merging and rectifying arrays in JavaScript
Problem
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments.
Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array.
The order here is not so important but the frequency of elements (which should be 1 for each element) is important.
For example, if the input to the function is −
onst arr1 = [6, 5, 2, 1, 8]; const arr2 = [3, 4, 6, 8, 9];
Then the output should be −
const output = [6, 5, 2, 1, 8, 3, 4, 9];
Example
Following is the code −
const arr1 = [6, 5, 2, 1, 8]; const arr2 = [3, 4, 6, 8, 9]; const mergeAndRectify = (arr1 = [], arr2) => { const { length: len1 } = arr1; const { length: len2 } = arr2; const res = []; let curr = 0; for(let i = 0; i < len1+len2; i++){ if(i >= len1){ curr = i - len1; if(!res.includes(arr1[curr])){ res.push(arr1[curr]); }; }else{ curr = i; if(!res.includes(arr2[curr])){ res.push(arr2[curr]); }; }; }; return res; }; console.log(mergeAndRectify(arr1, arr2));
Output
Following is the console output −
[ 3, 4, 6, 8, 9, 5, 2, 1 ]
- Related Articles
- Alternatively merging two arrays - JavaScript
- Merging sorted arrays together JavaScript
- Merging Arrays in Perl
- Merging two arrays in a unique way in JavaScript
- Merging nested arrays to form 1-d array in JavaScript
- Rectifying the time string in JavaScript
- Merging two sorted arrays into one sorted array using JavaScript
- Merging subarrays in JavaScript
- Merging two unsorted arrays in sorted order in C++.
- Merging boolean array with AND operator - JavaScript
- Merging elements of two different arrays alternatively in third array in C++.
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- AND product of arrays in JavaScript
- Comparing and filling arrays in JavaScript
- Adaptive Merging and Sorting in Data Structure

Advertisements