Unfortunately, splice is itself an O(n) "loop", so by using it in a nested loop, you are writing an O(n3) algorithm. Even for a 20 and 5 element array, respectively, it's going to take several hundred operations - not exactly the best approach. Using a hash is better - it will run in linear time.