Pergunta de entrevista da empresa Booking.com

I face only one question. If you are given 2 array A={3,1,2,4} B={1,4}. Write a program to compare two arrays and create another array which holds the common values between two array!

Respostas da entrevista

Sigiloso

25 de ago. de 2014

my @A = ( 3,1,2,4,3 ); my @B = ( 1,4,4,5,5 ); my $hash = +{}; my @C; for ( @A ) { $hash->{$_} = 1; } for ( @B ) { $hash->{$_} = 2 if $hash->{$_}; } for ( keys %$hash ) { push @C, $_ if $hash->{$_} > 1; }

2

Sigiloso

19 de dez. de 2014

my @a=(1,2,3,4,8,9,0); my @b=(2,5,6,8); my %a = map{$_ => undef} @a; my @ix = grep { exists( $a{$_} ) } @b;

2

Sigiloso

1 de mai. de 2014

in the previous post was a small error .. correct answer is reposted here #!/usr/bin/perl my @a = ('3','1','2','4','44'); my @b = ('23','44','1','4'); print "@a \n"; print "@b \\n"; my @res; my %a_hash =(); foreach (@a) { ++$a_hash{$_}; }; foreach (@b) { if ($a_hash{$_} >0) {push @res, $_; }; }; print "Common values @res\n";

1

Sigiloso

1 de dez. de 2018

3+2 = 1+4 =4+1 That's my take on it.

1

Sigiloso

22 de ago. de 2019

I tried rooftop slushie mentioned above and it was pretty helpful. I recommend it.

Sigiloso

16 de out. de 2019

Another great piece of content from Rooftop Slushie: bit.ly/faang100

Sigiloso

24 de jan. de 2020

Php: $a = [3,1,2,4]; $b = [1,4]; return array_intersect($a,$b); Js: const a = [3,1,2,4]; const b = [1,4]; return a.filter(value => b.includes(value)); Cheers

Sigiloso

3 de set. de 2015

my $a1 = [1, 2, 3, 4]; my $a2 = [1, 4, 5, 7]; my $newone; for my $num ( @{ $a2 } ){ push @{ $newone }, $num if $num ~~ @{ $a1 }; }

Sigiloso

10 de nov. de 2014

In Java Solution 1: Club both the array (say myArray)and pass that array into this method: getDistinctElements(myArray); public void getDistinctElements(int[] arr){ for(int i=0;i(Arrays.asList(myArray)).toArray(new String[myArray.length]); You can use your own logic to do so.

Sigiloso

1 de mai. de 2014

#!/usr/bin/perl my @a = ('3','1','2','4','44'); my @b = ('23','44','1','4'); my @res; my %a_hash =(); foreach (@a) { ++$a_hash{$_}; }; foreach (@b) { if ($a_hash{$_} >0) {push @res; }; }; print "Common values @res\n";

1

Sigiloso

21 de dez. de 2014

In Python: A=[3,1,2,4] B=[1,4] answer = [] for item in A: if item in B: answer.append(item) print answer

Sigiloso

9 de ago. de 2013

#!/usr/bin/perl my @a = ('3','1','2','4','44'); my @b = ('23','44','1','4'); my @res; foreach my $tmp1 (@a) { foreach my $tmp2 (@b) { push @res, $tmp1 if ($tmp1 == $tmp2); } } print "Common values @res\n";

3