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.
nice one, pretty good job.
like seriously speaking me too. he's really clever
This method is a must take home 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 don't get it ;D
super non-optimal
This comment is hidden because it contains spoiler information about the solution
Clever one, but between getting indexOf of a letter and lastIndexOf you'd be wasting too much execution-time. Great readibility however!
wow! great solution.
Yeah, that was my concern with this one as well. Though clever, it's a quadric runtime rather than the more brute force single loop through the list of characters, storing which ones were seen in a hash map, then incrementing a counter whenever you see a letter twice for the first time. It's more code and less clever, but would be linear complexity..
Great solution. Easy to understand.
because without
lastIndexOf
you'll get a sum of the total counts of each repeated charCan someone go through the filter method with me on this one? I am confused by the use of lastIndexOf. Why can't the condition just be if(arr.indexOf(val) !== i?
This is good one
Loading more items...