I guess on a daily basis I would go with Set approach for simplicity. I just wanted to add that in a interview I would probably try to come up with an O(n) solution, so something like:
This will work for all arrays as long as they only contain primitive values. Seeing as res[{ foo: 42 }] will access the index res['[object Object]'] no matter the object.
Nice article. :)
I guess on a daily basis I would go with Set approach for simplicity. I just wanted to add that in a interview I would probably try to come up with an O(n) solution, so something like:
Regarding comments about returning a string array independent of the input data:
Regarding comments about creating N number of objects:
This will work for all arrays as long as they only contain primitive values. Seeing as
res[{ foo: 42 }]
will access the indexres['[object Object]']
no matter the object.Yeah, fthis works but you are "mutating" the data itself