#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11109150 use warnings; my $x = 9; my %uniques; my @match; my @nomatch; while( ) { print; for my $element ( split ) { if( keys %uniques < $x ) { $uniques{ $element }++; } else { if( $uniques{ $element } ) { push @match, $element; } else { push @nomatch, $element; } } } } print "\nMATCH:\n@match\n\nNOMATCH:\n@nomatch\n"; __DATA__ 1 2 6 4 5 6 7 8 9 10 1 2 11 12 13 6 14 15 16 17 #### 1 2 6 4 5 6 7 8 9 10 1 2 11 12 13 6 14 15 16 17 MATCH: 1 2 6 NOMATCH: 11 12 13 14 15 16 17