|
|
|
|
|
by gfldex
2618 days ago
|
|
I gave this a shot in Perl 6 as can be found at https://gist.github.com/8a58c3af3520e127fe7852d834e5e0fa I build a very simple^1 non-balancing binary tree and skip any insertation that would reproduce a duplication. Since the tree is not balanced, the order of insertation is preserved. Search time goes up without a good balance but the tree will have the same amount of nodes than unique elements in the original array. Each element is 6 machine words plus some gc overhead. If it's faster or slower then a hash table depends on the ratio between elements in the array and the number of duplicates. 1) Perl 6 got a general compare operator that works well with a wide range of types. So I don't have to care about types at all. If I would have to care I could monkeytype the operator candidates for custom types into the language. |
|
The one-liner solution is: @data.unique(as => &fc)