What's the most efficient way to check for duplicates in an array of data using Perl?

前端 未结 7 1581
花落未央
花落未央 2020-12-05 14:26

I need to see if there are duplicates in an array of strings, what\'s the most time-efficient way of doing it?

7条回答
  •  北海茫月
    2020-12-05 15:01

    If you need the uniquified array anyway, it is fastest to use the heavily-optimized library List::MoreUtils, and then compare the result to the original:

    use strict;
    use warnings;
    use List::MoreUtils 'uniq';
    
    my @array = qw(1 1 2 3 fibonacci!);
    my @array_uniq = uniq @array;
    print ((scalar(@array) == scalar(@array_uniq)) ? "no dupes" : "dupes") . " found!\n";
    

    Or if the list is large and you want to bail as soon as a duplicate entry is found, use a hash:

    my %uniq_elements;
    foreach my $element (@array)
    {
        die "dupe found!" if $uniq_elements{$element}++;
    }
    

提交回复
热议问题