28
\$\begingroup\$

You will be given two Arrays / Lists / Vectors of non-negative integers A and B. Your task is to output the highest integer N that appears in both A and B, and is also unique in both A and B.


Test Cases:

A, B -> Output

[6], [1, 6] -> 6
[1, 2, 3, 4], [4, 5, 6, 7] -> 4
[0, 73, 38, 29], [38, 29, 73, 0] -> 73
[1, 3, 4, 6, 6, 9], [8, 7, 6, 3, 4, 3] -> 4
[2, 2, 2, 6, 3, 5, 8, 2], [8, 7, 5, 8] -> 5
[12, 19, 18, 289, 19, 17], [12, 19, 18, 17, 17, 289] -> 289
[17, 29, 39, 29, 29, 39, 18], [19, 19, 18, 20, 17, 18] -> 17
[17, 29, 39, 29, 29, 39, 18, 18], [19, 19, 18, 20, 17, 18] -> 17
\$\endgroup\$

36 Answers 36

0
\$\begingroup\$

Perl 5, 71 +2 (-ap) bytes

$F{$_}++for@F;map$\=$F{$_}^1||$L{$_}>1||$_<$\?$\:$_,keys%L;(%L,%F)=%F}{

Try It Online

\$\endgroup\$
0
\$\begingroup\$

Java 8, 134 132 bytes

Both args are List<Integer>

a->b->a.stream().filter(i->b.stream().filter(j->i-j==0).count()*a.stream().filter(j->i-j==0).count()==1).max(Integer::compare).get()
\$\endgroup\$
0
\$\begingroup\$

Ruby, 56 49 bytes

->a,b{((r=a+b).select{|e|r.count(e)==2}&a&b).max}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Axiom,126 74 68 58 bytes

q(a,b)==reduce(max,[x for x in a|count(x,a)*count(x,b)=1])

And with this I have less characters of JavaScript solutions and in general easier to read too (at last now...)

In mathematical words it would "find the max element of the list of the x in a, that are present 1 time only in a and 1 time only in b". This suppose that exist one element of the List [x for x in a|count(x,a)=1 and count(x,b)=1] as the question require. The other solution of 126 bytes

f(a,b)==(c:=reverse(sort(setIntersection(a,b)));for i in 1..#c repeat if count(c.i,a)=1 and count(c.i,b)=1 then return c.i;-1)

Results:

(6) -> q([1,2,3], [1,2,2,3])
   (6)  3
                                                    Type: PositiveInteger
(7) -> q([6], [1,6])
   (7)  6
                                                    Type: PositiveInteger
(8) -> q([1,2,3,4], [4,5,6,7])
   (8)  4
                                                    Type: PositiveInteger
(9) -> q([0,73,38,29], [38,29,73,0])
   (9)  73
                                                    Type: PositiveInteger
(10) -> q([1,3,4,6,6,9], [8,7,6,3,4,3])
   (10)  4
                                                    Type: PositiveInteger
\$\endgroup\$
0
\$\begingroup\$

PHP, 95 bytes

<?=max(array_intersect(e(A),e(B)));function e($a){return array_keys(array_count_values($a),1);}

Given A and B are defined constants holding each array.

\$\endgroup\$
0
\$\begingroup\$

APL NARS 40 28 chars

{⌈/g⍺∩(g←{⍵/⍨⊃1=+/(⊂⍵)=⍵})⍵}

g(⍵) return one list of elments of list ⍵ that appear in ⍵ 1 time only.

To the function for the question is assigned the name 'f' as (⍺)f(⍵) see below.

  f←{⌈/g⍺∩(g←{⍵/⍨⊃1=+/(⊂⍵)=⍵})⍵}
  1 2 3 f 1 2 2 3
3
  6 f 1 6
6
  1 2 3 4 f 4 5 6 7
4
  0 73 38 29 f 38 29 73 0
73
  1 3 4 6 6 9 f 8 7 6 3 4 3
4
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.