Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
7kyu and there's a built in method?
The key is line 3. If we just wanted ALL matches, we could just return arr.indexOf(val) !== arr.lastIndexOf(val) as that gives us, for example, ALL duplicate occurrences of "b" or "i" or whatever in the array.
However, the problem asks us to only return distinct duplicates, so we can't use the common pattern of indexOf !== lastIndexOf. We have to be a bit more clever than that.
In order to filter down to a distinct duplicate of the current index item, we essentially just want to return the last occurrence of the duplicate. The filter has to skip over duplicate values that have already been matched and find the last one.
Specifically we only want to return the current val in the filter/loop if (1) indexOf(val) has already appeared in the array but is not the current occurrence of val that we are looking at (arr.indexOf(val) !== i) AND (2) lastIndexOf(val) is the current occurrence (arr.lastIndexOf(val) === i).
In the words of Larry David, "pretty, pretty good". Very clever use of current index, indexOf() and lastIndexOf() to find the distinct duplicates. It took me some time to unpack the logic of the comparision but once I got it I realized how clever the technique here to resolve to the distinct duplicate items vs the collection of all of them.
I'm always looking for a way to use reduce and for the most obvious reduction challenges, its my goto, but I need to study this more to wrap my head around what's happening here. This takes reduction to a new level. I'd really like to use this pattern once I understand it.
This comment is hidden because it contains spoiler information about the solution
Same. This is by no means a 6kyu skill level challenge. The base solution here is a core fundamental javascript concept.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Thanks and noted on spoiler flag. Can you also share how you created the truth table? Looks like console.table output, which I also have not used but seems very useful.
What does each row represent and why are there 4 of them (ie, what is the input that the table is reflecting)?
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Practice. Don't listen to negative responses.
This comment is hidden because it contains spoiler information about the solution
Loading more items...