I'm stuck with this problem for 3 days now... Someone please help me.
Challenge 5
Construct a functionintersection
that compares input arrays and returns a new array with elements found in all of the inputs.
function intersection(arrayOfArrays) {
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
// should log: [5, 15]
You could reduce the array by filtering with just checking if the other array contains the value.
This works for arrays with unique values.
If no
initialValue
is provided, thenaccumulator
will be equal to the first value in the array, andcurrentValue
will be equal to the second.The callback
a.filter(v => b.includes(v))
filters array
a
. If the arrayb
includes the value ofa
, then this valuev
is included in theaccumulator
for the next iteration or as final result.accumulator currentValue new accumulator a b result -------------------- -------------------- -------------------- [ 5, 10, 15, 20] [15, 88, 1, 5, 7] [ 5, 15] [ 5, 15] [ 1, 10, 15, 5, 20] [ 5, 15]
function intersection(arrayOfArrays) {
return arrayOfArrays.reduce((a, b) => a.filter(v => b.includes(v)));
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
Reduce the arrays to a Map of counts, with the value as key. Spread the Map to entries. Use Array.filter()
on the Map's entries to remove all entries, which value is not equal to the arrayOfArrays
lenth. Extract the original number from the entries using Array.map()
:
function intersection(arrayOfArrays) {
return [...arrayOfArrays.reduce((r, s) => {
s.forEach((n) => r.set(n, (r.get(n) || 0) + 1));
return r;
}, new Map())]
.filter(([k, v]) => v === arrayOfArrays.length)
.map(([k]) => k);
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
First try to find out the intersection of two arrays which is the base problem
. Then try to build up for variable number of arrays passed as arguments for intersection. You can use reduce()
for doing that.
function intersectionOfTwoArrays(arr1, arr2)
{
return arr1.filter(x => arr2.some(y => y === x));
}
function intersection(...arrayOfArrays)
{
return arrayOfArrays
.reduce((a, b) => intersectionOfTwoArrays(a, b));
}
intersection(
[5, 10, 15, 20],
[15, 88, 1, 5, 7],
[1, 10, 15, 5, 20]
);
You can go through the first array in the array of arrays and check which of its value is present in all the other arrays.
Here is an example:
function intersection(input) {
let firstArray = input[0];
let restOfArrays = input.splice(1);
return firstArray.filter(v => restOfArrays.every(arr => arr.includes(v)));
}
const input = [[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]];
const result = intersection(input);
console.log(result);
Works with even if there is duplicate in same array.. like in my example added 5 twice in arrayEle[1];
var arrayEle = [[5, 10, 15, 20], [15, 88, 1, 5, 5], [1, 10, 15, 5, 20]]
var startIndex = 1;
var newArray = [];
for (var x = 0; x < arrayEle[0].length; x++) {
var temVal = 1;
var value;
for (var y = 1; y < arrayEle.length; y++) {
for (var z = 0; z < arrayEle[y].length; z++) {
if (arrayEle[y][z] == arrayEle[0][x]) {
temVal++;
value = arrayEle[y][z];
break;
}
}
}
if (temVal == arrayEle.length) {
newArray.push(value);
console.log(value);
}
}
console.log(newArray);
//log: [5, 15]
I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...
I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...
I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...
I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...
Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))
Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))
I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...
I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...