Perl (Scripting) - Duplicate elements in an array

by
Jeremy Canfield |
Updated: June 23 2021
| Perl (Scripting) articles
The following script demonstrates how to determine if there are duplicate elements in an array.
#!/usr/bin/perl
use strict;
use warnings;
my @fruits = qw(orange apple orange grape pineapple banana);
my %seen;
foreach my $fruit (@fruits) {
if ($seen{$fruit}++) {
print "$fruit has already been seen \n";
}
}
Running this script should return the following.
orange has already been seen
orange has already been seen
Did you find this article helpful?
If so, consider buying me a coffee over at