in reply to unique words

The code the OP provides is suggestive of an approach that works if the word list is sorted.

#!/usr/bin/perl use strict; use warnings; my $num_duplicates = 0; my $last = ''; while (<DATA>) { chomp; if ( $_ eq $last ) { $num_duplicates++; } else { print "$_\n"; $last = $_; } } print "There where $num_duplicates duplicates.\n"; __DATA__ apple banana banana cherry date date date elderberry

Output:

apple banana cherry date elderberry There where 3 duplicates.

Since the OP had the count variable, I kept it to track the total number of duplicates in the list.