use strict; use warnings; use feature qw(say); sub fairShuffle { my %count; ++$count{$_} for @_; my @result; for my $key (sort { $count{$a} <=> $count{$b} } keys %count) { my $newLength = @result + $count{$key}; for (1..$count{$key}) { splice @result, (($_-1) * $newLength / $count{$key}), 0, $key; } } return @result; } my @result = fairShuffle(('a') x 5, ('b') x 3, ('c') x 10, ('d') x 1, ('e') x 1); say "@result";