JavaScript Program to Create an Array of Unique Values From Multiple Arrays Using Set Object
Last Updated :
09 Oct, 2023
We have given multiple arrays consisting of numerical values, and our task is to create an output array that consists of unique values from the multiple input arrays. We will use the Set object in JavaScript language.
Example:
Input:
inputarray1: [1,2,3,4,5]
inputarray2: [4,5,6,7,8]
Output:
outputArray: [1,2,3,4,5,6,7,8]
Using Set and Spread Operator
In this approach, we are using the Set object and the spread operator to create a new array that contains unique values from the input arrays. Here, the function can handle any number of input arrays. The spread operator combines the input arrays into a single array and then we wrap with the Set object so that automatically duplicate elements are removed and unique elements are stored.
Syntax:
function function_name(...arrays) {
//statements
}
Example: This example shows the use of the above-explained approach.
Javascript
function mergeUsingSpread(
...inputArrays
) {
let uniqueValues = new Set();
inputArrays.forEach((arr) => {
arr.forEach((ele) => {
uniqueValues.add(ele);
});
});
return Array.from(uniqueValues);
}
let inputArray1 = [1, 2, 3, 4, 5];
let inputArray2 = [4, 5, 6, 7, 8];
let inputArray3 = [7, 8, 9, 10, 11];
let outputArray = mergeUsingSpread(
inputArray1,
inputArray2,
inputArray3
);
console.log(outputArray);
|
Output
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11
]
Using the Concat method and Set
In this approach, we have used the concat method in the function merge. Using concat, we are handling multiple input arrays and merging them into a single array. Using the set, we are then removing duplicate elements and converting them to arrays, then printing the output.
Syntax:
let newArray = Array.prototype.concat.apply([], arguments)
Example: This example shows the use of the above-explained approach.
Javascript
function mergeUsingConcat() {
let allMergedArr =
Array.prototype.concat.apply(
[],
arguments
);
return Array.from(
new Set(allMergedArr)
);
}
let inputArray1 = [1, 2, 3, 4, 5];
let inputArray2 = [4, 5, 6, 7, 8];
let inputArray3 = [7, 8, 9, 10, 11];
let outputArray = mergeUsingConcat(
inputArray1,
inputArray2,
inputArray3
);
console.log(outputArray);
|
Output
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11
]
Using Reduce method with the Set
In this approach, we are using the reduce method to and the forEach method to iterate over all the input arrays, and all this is stored in the set, where the unqualified values are considered. Later, we will convert this set into an array and print the result.
Syntax:
return Array.prototype.reduce.call(arguments, function (res, currentArr) {
// Iterate through the current array
}, new Set());
Example: This example shows the use of the above-explained approach.
Javascript
function mergeUsingReduce() {
return Array.prototype.reduce.call(
arguments,
function (res, currentArr) {
currentArr.forEach(
function (ele) {
res.add(ele);
}
);
return res;
},
new Set()
);
}
let inputArray1 = [1, 2, 3, 4, 5];
let inputArray2 = [4, 5, 6, 7, 8];
let inputArray3 = [7, 8, 9, 10, 11];
let tempresult = mergeUsingReduce(
inputArray1,
inputArray2,
inputArray3
);
let outputArray =
Array.from(tempresult);
console.log(outputArray);
|
Output
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11
]
Share your thoughts in the comments
Please Login to comment...