my @a = ( 1, 2, 3 );
my @b = ( 2, 3, 1 );
my @c = ( 1, 2, 4 );
print "(@a) and (@b) are equal sets.\n" if identical( \@a, \@b );
print "(@a) and (@c) are equal sets.\n" if identical( \@a, \@c );
print "(@b) and (@c) are equal sets.\n" if identical( \@b, \@c );
sub identical {
my( $left, $right ) = @_;
return 0 if scalar @$left != scalar @$right;
my %hash;
@hash{ @$left, @$right } = ();
return scalar keys %hash == scalar @$left;
}
####
use List::MoreUtils qw/any/;
my @a = ( 1, 2, 3 );
my @b = ( 2, 3, 1 );
my @c = ( 1, 2, 4 );
print "(@a) and (@b) are equal sets.\n" if identical( \@a, \@b );
print "(@a) and (@c) are equal sets.\n" if identical( \@a, \@c );
print "(@b) and (@c) are equal sets.\n" if identical( \@b, \@c );
sub identical {
my( $left, $right ) = @_;
return 0 if scalar @$left != scalar @$right;
my( %left, %right );
$left{$_}++ for @$left;
$right{$_}++ for @$right;
return 0 if any{
! exists $left{$_}
|| ! exists $right{$_}
|| $left{$_} != $right{$_}
} keys %left, keys %right;
return 1;
}
####
use List::MoreUtils qw/any/;
my @a = ( 1, 2, 3 );
my @b = ( 2, 3, 1 );
my @c = ( 1, 2, 4 );
print "(@a) and (@b) are equal sets.\n" if identical( \@a, \@b );
print "(@a) and (@c) are equal sets.\n" if identical( \@a, \@c );
print "(@b) and (@c) are equal sets.\n" if identical( \@b, \@c );
sub identical {
my( $left, $right ) = @_;
return 0 if scalar @$left != scalar @$right;
my( %bag );
$bag{$_}++ for @$left;
$bag{$_}-- for @$right;
return 0 if any{ $bag{$_} } keys %bag;
return 1;
}
####
use Set::Bag;
my @a = ( 1, 2, 3 );
my @b = ( 2, 3, 1 );
my @c = ( 1, 2, 4 );
print "(@a) and (@b) are equal sets.\n" if identical( \@a, \@b );
print "(@a) and (@c) are equal sets.\n" if identical( \@a, \@c );
print "(@b) and (@c) are equal sets.\n" if identical( \@b, \@c );
sub identical {
my ( $left, $right ) = @_;
return 0 if scalar @$left != scalar @$right;
my $bag_a = Set::Bag->new;
my $bag_b = Set::Bag->new;
$bag_a->over_delete(1);
$bag_b->over_delete(1);
$bag_a->insert( $_ => 1 ) for @$left;
$bag_b->insert( $_ => 1 ) for @$right;
return 1 if $bag_a->difference($bag_b) eq '()'
&& $bag_b->difference($bag_a) eq '()';
return 0;
}