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
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:
Which are you looking for?
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.
maybe there is an easier, better way, but would this do it? (first thing that came to mind)
so array lenght could be 4 or 3 or 2 , but both have same lenght
i think he's looking for the unique values that are present in both arrays ??
Mark is right, create a
new Set()
for each to remove duplicates and compare the length of each alsoI 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
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
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
yea, okay. but with my code the first will also return 3. is that correct?
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
okay... just asking. thought it had to be the same code for all examples
I separated with comments
🙂
okay
you can remove the " + " before " a " btw. that was a typo
It is working well. Thank you so much 🙂