Recoded duplicate removal function, theorectical runtime is still O(n^2) but in practice is much faster