use strict; use warnings; my %hash; while(){ chomp; $hash{$_}++ for split; } print join $/ => sort keys %hash; print $/,$/, join $/ => grep {$_ if $hash{$_} > 1} sort keys %hash; __DATA__ teacher students professor students school class teacher desk table chalk borad class school teacher