6

FAQ: In Raku, how to remove duplicates from a list to only get unique values?

my $arr = [1, 2, 3, 2, 3, 1, 1, 0];
# desired output [1, 2, 3, 0]

1 Answer 1

8
  1. Use The built-in unique
@arr.unique  # (1 2 3 0)
  1. Use a Hash (alias map, dictionary)
my %unique = map {$_ => 1}, @arr;
%unique.keys;  # (0 1 2 3) do not rely on order
  1. Use a Set: same method as before but in one line and optimized by the dev team
set(@arr).keys
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the .unique solution is probably the most performant and preserves order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.