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

前端 未结 7 1597
花落未央
花落未央 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:04

    One of the things I love about Perl is it's ability to almost read like English. It just sort of makes sense.

    use strict;
    use warnings;
    
    my @array = qw/yes no maybe true false false perhaps no/;
    
    my %seen;
    
    foreach my $string (@array) {
    
        next unless $seen{$string}++;
        print "'$string' is duplicated.\n";
    }
    

    Output

    'false' is duplicated.

    'no' is duplicated.

提交回复
热议问题