如何删除列表中的重复项 (Raku)

tin*_*ino 5 raku

FAQ:在 Raku 中,如何从列表中删除重复项以仅获取唯一值?

my $arr = [1, 2, 3, 2, 3, 1, 1, 0];
# desired output [1, 2, 3, 0]
Run Code Online (Sandbox Code Playgroud)

tin*_*ino 7

  1. 使用内置的独特
@arr.unique  # (1 2 3 0)
Run Code Online (Sandbox Code Playgroud)
  1. 使用哈希(别名映射、字典)
my %unique = map {$_ => 1}, @arr;
%unique.keys;  # (0 1 2 3) do not rely on order
Run Code Online (Sandbox Code Playgroud)
  1. 使用Set:与以前相同的方法,但在一行中并由开发团队优化
set(@arr).keys
Run Code Online (Sandbox Code Playgroud)

  • 请注意,“.unique”解决方案可能是性能最好的*并且*保留了顺序。 (6认同)