Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #312 List equality

Set b to true if the lists p and q have the same size and the same elements, false otherwise.

  if (size(p,1) /= size(q,1)) then
    b = false
  else
    b = all (p == q)
  end if
import "slices"
b := slices.Equal(p, q)
import java.util.ArrayList;
boolean b = p.size() == q.size();

if(b) {
	for(T item : p) {
		if(q.contains(item) == false) {
			b = false;
			break;
		}
	}
	
	if(b) {
		for(T item : q) {
			if(p.contains(item) == false) {
				b = false;
				break;
			}
		}
	}
}
classes
  b := (p.count = q.count);
  if b then for i := 0 to p.count-1 do 
    if (p.items[i] <> q.items[i]) then
    begin
      b := false;
      break;
    end;
use v5.10.1;
no warnings 'experimental::smartmatch';
$b = @array1 ~~ @array2;
use v5.10;
use smart::match;
my $b = \@array1 |M| \@array2;
use v5.10;
use strict;
sub lcomp {
    my ($a, $b) = @_;

    return 0 if ( @$a != @$b ); # lengths different

    my $matched = 1;
    for (my $i=0; $i < @$a; $i++) {
        return 0 unless $a->[$i] == $b->[$i];
    }

    return 1;
}
use Data::Compare;
$b = Compare( \@p, \@q );
b = p == q
b = p == q
b = p == q;

New implementation...
< >
programming-idioms.org