Ad
  • Custom User Avatar

    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).

  • Custom User Avatar

    What more do you need? That's already there and it seems clear enough imo.

  • Custom User Avatar

    That's a problem with your code, not a kata issue:

    The string may contain any of the 128 ASCII characters.