Is the function actually slow? You seem to be using arrays, which means you already crossed the biggest tortoise trap. After that it's basic js optimization. It's still unclear to me, on the surface, what's the input and what's the expected output with each function. Your question maybe better suited to
Code Review. One thing I'll note
unique
can be rewritten with
Set
, which has better support for unique data. —
TheMaster 36 secs ago