compare arrays

Hello, How to : compare arrays how many same values in it? here is an example, but not working: https://codepen.io/lanszelot/pen/PoaEorm the arrays value could be -red red red red- , or -blue red yellow blue- or any variation. if one array have more same value /like red red red/ and the other one have only one red it have to count once only. arrays lenght is same. only the values could be different or equal.
16 Replies
13eck
13eck2y ago
Are you looking for a complete match, element by element? Or are you wanting to see if the arrays have the same number of array elements? For example, would any of these pass the test you're trying to write:
// this isn't strictly equal because the elements are in a different order
const arr1 = ["red", "blue", "green", "blue"];
const arr2 = ["blue", "red", "blue", "green"];

// they both have the same content, but not in the same
// quantity
const arr3 = ["red", "blue", "green", "blue"];
const arr4 = ["red", "red", "blue", "green"];

// these have the same elements in the same
// quantity and same place
const arr5 = ["red", "blue", "green", "blue"];
const arr6 = ["red", "blue", "green", "blue"];
// this isn't strictly equal because the elements are in a different order
const arr1 = ["red", "blue", "green", "blue"];
const arr2 = ["blue", "red", "blue", "green"];

// they both have the same content, but not in the same
// quantity
const arr3 = ["red", "blue", "green", "blue"];
const arr4 = ["red", "red", "blue", "green"];

// these have the same elements in the same
// quantity and same place
const arr5 = ["red", "blue", "green", "blue"];
const arr6 = ["red", "blue", "green", "blue"];
Which are you looking for?
lanszelot
lanszelot2y ago
no, í do not want to compare the arrays. I want to compare the values. How many same values in the arrays so all of them the arrays are changeing all the time, and the arrays lenght too.
MarkBoots
MarkBoots2y ago
maybe there is an easier, better way, but would this do it? (first thing that came to mind)
[...new Set(aTomb)].reduce((a, c) => a + [...new Set(bTomb)].includes(c), 0)
[...new Set(aTomb)].reduce((a, c) => a + [...new Set(bTomb)].includes(c), 0)
lanszelot
lanszelot2y ago
so array lenght could be 4 or 3 or 2 , but both have same lenght
13eck
13eck2y ago
If "all of them" then your code looks like it works Igonre that, I'm wrong
MarkBoots
MarkBoots2y ago
i think he's looking for the unique values that are present in both arrays ??
13eck
13eck2y ago
Mark is right, create a new Set() for each to remove duplicates and compare the length of each also
lanszelot
lanszelot2y ago
I am testing Mark´s code. It looks good for a first sight. But I have to test it. Thanks for the help . It takes a few minutes while I testing
MarkBoots
MarkBoots2y ago
ah, i think im not right. the first example has 2 duplicates in both arrays and they still return 4. Not sure about the task now
lanszelot
lanszelot2y ago
your code looks right. I put it end of the line - on the link what I shared. be carefull , all 3 code have own arrays so your code starting after 14th line with new arrays and it is good but I have to test with more arrrays
MarkBoots
MarkBoots2y ago
yea, okay. but with my code the first will also return 3. is that correct?
lanszelot
lanszelot2y ago
your code return 3rd number which is 3. If you check the arrays the 17th line have "red" which is not in the 16th, so 3 is perfect return
MarkBoots
MarkBoots2y ago
okay... just asking. thought it had to be the same code for all examples
lanszelot
lanszelot2y ago
I separated with comments 🙂
MarkBoots
MarkBoots2y ago
okay you can remove the " + " before " a " btw. that was a typo
lanszelot
lanszelot2y ago
It is working well. Thank you so much 🙂
Want results from more Discord servers?
Add your server