in reply to Better way of writing "find the missing number(s)"
Hi pritesh_ugrankar.
A hash is usually the best-suited tool for finding out whether elements in one list are in another. Add a key to the hash for each element in the list you are checking, then see if a key exists for each element in the list to be matched.
use strict; use warnings; use feature 'say'; my @x = ( -2, 2, 3, 8, 10 ); my %y = map { $_ => 1 } @x; say "missing: $_" for grep { not exists $y{ $_ } } ( -2 .. 10 );
perl 1186862.pl missing: -1 missing: 0 missing: 1 missing: 4 missing: 5 missing: 6 missing: 7 missing: 9
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Better way of writing "find the missing number(s)"
by stevieb (Canon) on Apr 03, 2017 at 22:20 UTC | |
|
Re^2: Better way of writing "find the missing number(s)"
by pritesh_ugrankar (Monk) on Apr 03, 2017 at 18:37 UTC | |
by 1nickt (Canon) on Apr 03, 2017 at 20:33 UTC |