DEV Community

Discussion on: #22 - Find the unique number CodeWars Kata (6 kyu)

Collapse
 
lexlohr profile image
Alex Lohr • Edited

You can use the array methods find and lastIndexOf/indexOf to do this in a more direct manner:

return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))
Enter fullscreen mode Exit fullscreen mode

find has the added advantage over filter that it stops iterating after the first match. Sorting the array will only work for numbers, strings or objects that can be coerced to either.

Collapse
 
cesar__dlr profile image
Cesar Del rio

Great solution for optimizing the code, thanks bro! πŸ™ŒπŸ”₯