Find the unique twins

28

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

Mr. Xcoder

Posted 2017-11-02T17:59:52.973

Reputation: 39 774

Answers

7

Jelly, 7 bytes

fċ@ÐṂ;Ṁ

Try it online!

How it works

fċ@ÐṂ;Ṁ  Main link. Left argument: A. Right argument: B

f        Filter; keep those elements of A that appear in B.
   ÐṂ    Yield all elements of the result for which the link to left yields a
         minimal value (2).
 ċ@        Count the occurrences of the element...
     ;     in the concatenation of A and B.
      Ṁ  Take the maximum.

Dennis

Posted 2017-11-02T17:59:52.973

Reputation: 196 637

7

Bash + coreutils, 49 bytes

U()(sort -rn|uniq -u$1)
(U<<<$1;U<<<$2)|U D|sed q

Thanks to @seshoumara for golfing off 1 byte!

Try it online!

How it works

uniq takes sorted input and performs one or several actions, depending on the command-line flags.

U<<<$1 and U<<<$2 call the function U with the first and second command-line argument as input. For each one, sort -rn|uniq -u is executed, sorting the input numerically (-n) and in descending order (-r) for uniq, which will print only unique lines (-u).

The output of both (unique elements of each array) is concatenated and piped to U D, i.e.,
sort -rn|uniq -uD. This time, uniq will only print duplicate lines (-D), and only the first repetition of each.

While the man page says it will print all repetitions, the added -u causes -D to print only the first occurrence of duplicated lines. This behavior is normally achieved with uniq -d.

Finally, sed q quits immediately, reducing its input (unique elements of both arrays) to its first line. Since the output was sorted in descending order, this is the maximum.

Dennis

Posted 2017-11-02T17:59:52.973

Reputation: 196 637

6

Python 3, 61 56 54 bytes

Saved 7 bytes thanks to @Mr.Xcoder, @pizzapants184 and @ovs

lambda a,b:max(f*(a.count(f)==b.count(f)<2)for f in a)

Try it online!

Halvard Hummel

Posted 2017-11-02T17:59:52.973

Reputation: 3 131

Beat me by a minute! – pizzapants184 – 2017-11-02T18:11:54.203

57 bytes – pizzapants184 – 2017-11-02T18:14:18.287

56 bytes – Mr. Xcoder – 2017-11-02T18:15:31.933

54 bytes – ovs – 2017-11-02T21:59:50.870

6

Pyth, 12 9 bytes

Try it

eS@Fm.m/d

Saved 3 bytes thanks to Mr. Xcoder.

Explanation

eS@Fm.m/d
    m  /d   Count the occurrences of each element.
     .m     Take only those that appear the minimum number of times.
  @F        Apply the above to A and B and take the intersection.
eS          Take the largest.

user48543

Posted 2017-11-02T17:59:52.973

Reputation:

Nice! My solution was also 12 bytes.

– Mr. Xcoder – 2017-11-02T18:24:05.740

Golfing my solution down a bit, 9 bytes (using eS@Fm.m/d) and taking input as a list of two lists.

– Mr. Xcoder – 2017-11-02T18:29:31.427

@Mr.Xcoder That seems different enough to be its own answer. – None – 2017-11-02T18:53:35.657

Since I am the OP of the challenge, I am reluctant about posting it. You can just use it and give credit, mentioning your current approach as an alternative (if want to of course) – Mr. Xcoder – 2017-11-02T18:54:55.590

5

05AB1E, 9 8 bytes

-1 byte thanks to Erik the Outgolfer

εТÏ}`Ãà

Try it online!

ε   }    # For each input
 Ð¢Ï     #   Get the unique elements
     `Ã  # Keep only the elements that appear in both lists
       à # Keep the greatest element
         # Implicit print

Riley

Posted 2017-11-02T17:59:52.973

Reputation: 11 345

5

Husk, 7 bytes

→►≠OfEΠ

Takes input as a list of two lists, works for any number of lists as well (returning the highest number that occurs exactly once in each, if possible). Try it online!

Explanation

This is the first Husk answer to (ab)use the new "maximum by" function .

→►≠OfEΠ  Implicit input, say [[3,2,1,3],[1,2,3,4]]
      Π  Cartesian product: [[3,1],[2,1],[3,2],[2,2],[1,1],[3,3],[1,2],[3,1],[3,4],[2,3],[1,3],[3,2],[2,4],[3,3],[1,4],[3,4]]
    fE   Keep those that have equal elements: [[2,2],[1,1],[3,3],[3,3]]
   O     Sort: [[1,1],[2,2],[3,3],[3,3]]
 ►≠      Find rightmost element that maximizes number of other elements that are not equal to it: [2,2]
→        Take last element: 2

Zgarb

Posted 2017-11-02T17:59:52.973

Reputation: 39 083

4

Bash + coreutils, 60 bytes

f()(sort -rn<<<"$1"|uniq -u);grep -m1 -wf<(f "$1") <(f "$2")

Try It Online

Bash, 89 bytes

c()(for e;{((e^$1||r++,2^r));});for x in $1 $2;{((x<r))||c $x $1||c $x $2||r=$x;};echo $r

TIO

Nahuel Fouilleul

Posted 2017-11-02T17:59:52.973

Reputation: 5 582

1Use sort -rn with sed q at the end instead of tail -1 to shave 1 byte. Great find with grep -wf btw. +1 – seshoumara – 2017-11-04T06:48:33.717

@seshoumara, thank you for the tip, in fact i could shave 3 bytes with -m1 grep option. – Nahuel Fouilleul – 2017-11-04T19:57:26.563

3

Jelly, 11 bytes

ċ@Ị¥Ðf`€f/Ṁ

Try it online!

Erik the Outgolfer

Posted 2017-11-02T17:59:52.973

Reputation: 38 134

Clever usage of ```! – Mr. Xcoder – 2017-11-02T18:13:16.433

3

J, 23 bytes

>./@([-.-.)&(-.-.@~:#])

(-.-.@~:#]) removes from a list any repeated elements

& do this to both args

([-.-.) We want A intersect B. This is the equivalent phrase: "A minus (A minus B)"

>./ Take the max

Try it online!

Jonah

Posted 2017-11-02T17:59:52.973

Reputation: 8 729

Equivalently, you can replace the intersection part with e.~#]. Golfing this has proven to be tough... I tried using /.-key to no success (((1=#)/.~#~.) for the first part which is 2 bytes longer by my count) – cole – 2017-11-03T16:37:33.697

@cole, yeah, I tried a key approach as well, and also one with self-classify. I wasn't able to beat my submission above with any other approach though. – Jonah – 2017-11-03T16:38:58.287

2

PowerShell, 94 bytes

param($a,$b)filter f($x){$x|group|?{$_.count-eq1}}
(f($a|sort|?{$_-in((f $b).Name)}))[-1].Name

Try it online!

Takes input $a and $b as arrays. Constructs a filter that groups the input array elements together and pulls out only those with a count -equal to 1 (i.e., only those that are unique in the input array).

The next line then constructs the algorithm. First we sort $a, then pull out those that are -in the unique items of $b. Those are then themselves unique-ified, the largest [-1] is chosen, and we take the .Name thereof. That's left on the pipeline and output is implicit.

AdmBorkBork

Posted 2017-11-02T17:59:52.973

Reputation: 41 581

2

Javascript (ES6), 102 86 75 71 bytes

a=>b=>Math.max(...a.map(e=>(g=x=>x.map(y=>y-e||x--,x=1)|!x)(a)*g(b)*e))

Thanks @justinMariner for getting from 102 to 86

Thanks @tsh for getting from 86 to 75

Thanks @Arnauld for getting from 75 to 71

Try it online!

Nate

Posted 2017-11-02T17:59:52.973

Reputation: 121

Welcome to PPCG! As far as I can tell this doesn't make sure that e shows up only once in a and b. – Martin Ender – 2017-11-02T22:21:18.600

@MartinEnder Thanks! Edited the answer to reflect the details I missed! – Nate – 2017-11-02T22:48:09.733

Hm, wouldn't this now return 3 for f([1,2,3,3], [2,4])? It seems you only check that the value appears exactly twice in the concatenation of a and b, but not that those two occurrences weren't from the same original array. – Martin Ender – 2017-11-02T22:57:09.593

Yeah, you're right. I would propose a test case for that. – Nate – 2017-11-02T22:59:21.623

@MartinEnder Thanks for the help. This passes all the test cases including one you added – Nate – 2017-11-02T23:26:54.470

1

I never thought of using lastIndexOf like that, that's pretty clever. You can get this down to 86 bytes: Try it online!. Check out the JS tips for more.

– Justin Mariner – 2017-11-03T00:03:07.553

1It seems that using (g=x=>x.filter(y=>y==e).length==1) is shorter. – tsh – 2017-11-03T01:34:10.853

a=>b=>Math.max(...a.map(e=>(g=x=>x.filter(y=>y==e).length==1)(a)*g(b)*e)) should work. (There's an edge case with 0, but if the resulting array contains only zeros, then zero must be the correct answer.) – Arnauld – 2017-11-03T14:25:17.557

@Arnauld Since the rules indicate there will be one such unique integer, this case is ok.(But it's same number of bytes as above) – Nate – 2017-11-03T14:37:00.393

It's 73 bytes, but Stack Exchange inserted two ghost characters after length==1. – Arnauld – 2017-11-03T14:42:49.083

1

I think this one is also passing all edge cases (71 bytes).

– Arnauld – 2017-11-03T14:46:41.583

x-=y==e is actually one byte shorter. – Arnauld – 2017-11-03T17:42:07.500

2

Haskell, 57 53 bytes

x?y|let v!x=filter(==v)x==[v]=maximum[a|a<-x,a!x,a!y]

Try it online!

UPD: Thanks @Laikoni

user28667

Posted 2017-11-02T17:59:52.973

Reputation: 579

Welcome to PPCG and Haskell golfing in particular! This is a nice first answer! Two small things: You can also declare f as infix operator and write [1|...]==[1] instead of sum[1|...]==1 to save some bytes. – Laikoni – 2017-11-03T16:03:22.943

In case you haven't seen them already, here are some links which might be interesting: Our collection of golfing tips in Haskell, the guide to golfing rules in Haskell and Of Monads and Men, our Haskell chat room.

– Laikoni – 2017-11-03T16:07:39.077

1

In-lining ! with and saves two more bytes: Try it online!

– Laikoni – 2017-11-03T21:50:57.440

2

C# (.NET Core), 85 84 bytes

x=>y=>x.Where(a=>x.Count(b=>b==a)<2).Intersect(y.Where(a=>y.Count(b=>b==a)<2)).Max()

Try it online!

Naive solution with LINQ (using System;using System.Linq; +31chars so 116 bytes with header)

84! I forgot currying!

aloisdg moving to codidact.com

Posted 2017-11-02T17:59:52.973

Reputation: 1 767

2

F# (.NET Core), 117 115 114 111 108 bytes

115 114 bytes

Another solution with countBy this time:

let u x=x|>Seq.countBy id|>Seq.filter(fun a->snd a=1)|>Seq.map fst|>set
let f x y=Set.intersect(u x)(u y)|>Seq.max

Try it online!

117 111 bytes

let u x=x|>Seq.filter(fun a->x|>Seq.filter((=)a)|>Seq.length=1)|>set
let f x y=Set.intersect(u x)(u y)|>Seq.max

Try it online!

100% F#! Any help is welcome!

6 byte won thanks to prefix notation!

108 bytes

let f a b=Set.intersect(set a)(set b)|>Seq.filter(fun x->a@b|>Seq.filter(fun y->y=x)|>Seq.length<3)|>Seq.max

@ is the concat function! Thank you @Ayb4btu for this algorithm.

Try it online!

aloisdg moving to codidact.com

Posted 2017-11-02T17:59:52.973

Reputation: 1 767

2

Wolfram Language (Mathematica), 40 bytes

Max@Cases[Tally@#⋂Tally@#2,{x_,1}:>x]&

Try it online!

How it works

Tally@# gives a list of the unique elements of the first input, together with their counts: e.g., Tally[{2,2,2,6,3,5,8,2}] yields {{2,4},{6,1},{3,1},{5,1},{8,1}}.

Tally@#2 does the same for the second list, and finds pairs present in both. Then we pick out (with Cases) pairs ending in 1, taking the first element of each result, which gives us a list of all the unique twins. Finally, Max returns the largest unique twin.

Misha Lavrov

Posted 2017-11-02T17:59:52.973

Reputation: 4 846

2

Röda, 48 bytes

{m={|n|sort|count|[_]if[_=n]};[_()|m 1]|m 2|max}

Try it online!

Inspired by jq170727's jq answer.

Explanation:

{ /* Anonymous function, takes input from the stream */
  m={|n|        /* Local function m with parameter n: */
    sort|count| /*   Count unique values in the stream */
    [_]if[_=n]  /*   For each value, push it to the stream if its count is n */
  };
  [      /* For each list in the stream: */
    _()| /*   Flat it (push its values to the stream) */
    m 1  /*   Push values that appear only once to the stream */
  ]|
  m 2|   /* Push values that appear twice to the stream */
  max    /* Find the max value in the stream */
}

fergusq

Posted 2017-11-02T17:59:52.973

Reputation: 4 867

2

Pip, 17 16 bytes

MX{_Na=_Nb=1FIa}

This is a function that takes two lists as arguments. Try it online!

Explanation

  {            }  Define function, args are a & b:
            FIa    Filter elements of a on this function:
   _Na              Count of element in a
      =_Nb          equals count of element in b
          =1        equals 1
                  This gives a function that returns a list of unique twins
MX                Modify it to take the max and return that instead

DLosc

Posted 2017-11-02T17:59:52.973

Reputation: 21 213

2

APL (Dyalog), 18 chars = 23 bytes*

A full program body. Prompts for list of lists from STDIN. Works with any number of lists. Outputs to STDOUT.

⌈/∊∩/{⊂⍺⍴⍨1=≢⍵}⌸¨⎕

Try it online!

 prompt for evaluated input from STDIN

{}⌸¨ for each list, call the following function for each unique element in that list, using the unique element as left argument () and the list of indices of its occurrence as right argument ():

≢⍵ the tally of indices (i.e. the number of occurrences)

1= equal to 1

⍺⍴⍨ use that to reshape the specific element (i.e. gives empty list if non-unique)

Now we have two lists of unique elements for each input list (although each element is a list, and there are empty lists as residue from the non-unique elements).

∩/ intersection (reduction)

ϵnlist (flatten)

⌈/ max (reduction)


* in Classic, counting as ⎕U2338.

Adám

Posted 2017-11-02T17:59:52.973

Reputation: 37 779

1

MATL, 13 bytes

,iSY'1=)]X&X>

Try it online! Or Verify all test cases.

Explanation

,      % Do twice
  i    %   Take input: array
  S    %   Sort array
  Y'   %   Run-length encode: pushes array of values and array of run lengths
  1=   %   Compare each run length with 1
  )    %   Use as logical index. This keeps only values that have appeared once
]      % End
X&     % Intersection of the two arrays
X>     % Maximum of array. Implicitly display

Luis Mendo

Posted 2017-11-02T17:59:52.973

Reputation: 87 464

1

PHP, 98 bytes

<?foreach(($c=array_count_values)($_GET[a])as$a=>$n)$n-1||$c($_GET[b])[$a]-1||$a<$r||$r=$a;echo$r;

Provide arrays as GET parameters a and b.

Titus

Posted 2017-11-02T17:59:52.973

Reputation: 13 814

Think you could swap those GET params for constants. – Progrock – 2017-11-06T08:37:54.097

@Progrock That´s no valid input method. – Titus – 2017-11-06T12:39:17.143

The question phrases that A and B are given as arrays. And says any reasonable input and output method.... not that I can follow that link easily. Really like your recipe. (Chokes in obsolete Php 5.6 though.) – Progrock – 2017-11-06T16:53:54.903

1

Java 8, 133 bytes

a->b->{long r;for(java.util.Collections c=null;;a.remove(r))if(b.contains(r=c.max(a))&c.frequency(a,r)*c.frequency(b,r)==1)return r;}

Explanation:

Try it here.

a->b->{                  // Method with two ArrayList<Long> parameters and long return-type
  long r;                //  Result-long
  for(java.util.Collections c=null; 
                         //  Create a java.util.Collections to save bytes
      ;                  //  Loop indefinitely
       a.remove(r))      //    After every iteration, remove the current item
    if(b.contains(r=c.max(a)) 
                         //   If the maximum value in `a` is present in `b`,
       &c.frequency(a,r)*c.frequency(b,r)==1)
                         //   and this maximum value is unique in both Lists:
      return r;          //    Return this value
                         //  End of loop (implicit / single-line body)
}                        // End of method

Kevin Cruijssen

Posted 2017-11-02T17:59:52.973

Reputation: 67 575

1

R, 73 bytes

function(A,B)max(setdiff(intersect(A,B),c(A[(d=duplicated)(A)],B[d(B)])))

Try it online!

Computes A intersect B, then the maximum of the difference between that and the duplicated elements of A and B.

Giuseppe

Posted 2017-11-02T17:59:52.973

Reputation: 21 077

1

JavaScript ES5, 122 121 114 bytes

function f(a,b){for(i=a.sort().length;--i+1;)if(a[i]!=a[i+1]&&a[i]!=a[i-1]&&!(b.split(a[i]).length-2))return a[i]}

I'm new here, so I don't really know if I can remove the function definition and just put its contents (which would save me 17 bytes)

Here's the working example: 122 121 114

122 to 121 bytes: Wrapping initialization in a for

121 to 114 bytes: b has to be a string

Piyin

Posted 2017-11-02T17:59:52.973

Reputation: 111

2Welcome to PPCG! You cannot remove the function definition, but you might be able to use a lambda function instead (I don't know JS so I cannot help you with that). – Mr. Xcoder – 2017-11-03T16:18:02.740

Thanks for clearing that out. I don't think lambda functions can save any characters, but I'll try. Also, since you say "Any reasonable Input and Output method / format is allowed", could I accept an string as b and save b=''+b,? – Piyin – 2017-11-03T16:24:07.100

I did manage to get to 115 bytes although I don't know JavaScript: f=(a,b)=>{for(b=''+b,i=a.sort().length;--i+1;)if(a[i]!=a[i+1]&&a[i]!=a[i-1]&&!(b.split(a[i]).length-2))return a[i]}. – Mr. Xcoder – 2017-11-03T16:24:09.437

1Yeah sure a string input would be fine – Mr. Xcoder – 2017-11-03T16:24:35.407

But that JavaScript you came up with would be ES5, not ES6. An ES6 answer is already posted, which is why I posted the ES5. And thanks for answering the second question hehe – Piyin – 2017-11-03T16:25:20.713

As I said above, I don't know Javascript at all, so I don't know the appropriate versions. Anyway it would be 112 bytes with lambdas taking input via currying, but IDK if that would work in ES5 too.

– Mr. Xcoder – 2017-11-03T16:26:48.970

Yes, I understand that, I was just telling you why that wouldn't work hehe The function definition in ES5 doesn't support the arrow: https://medium.com/@manojsinghnegi/es5-vs-es6-with-example-code-9901fa0136fc#702a I reduced it to 114, though, thanks to your clarification

– Piyin – 2017-11-03T16:45:07.947

1

C# (.NET Core), 66+31=97 65+31=96 bytes

a=>b=>a.Intersect(b).Where(x=>a.Concat(b).Count(y=>y==x)<3).Max()

Try it online!

+31 bytes for using System;using System.Linq;

I took inspiration from @aloisdg's answer. However, instead of searching for unique values in both arrays, I inverted the order of operations so that intersect is first, and then find the max value of the items that occur twice when the arrays are concatenated and are in their intersect. I can use <3 as Count will be at least 2 for any value, as it will be in both arrays.

Acknowledgements

-1 byte thanks to @aloisdg and his suggestion to use Func currying.

Ayb4btu

Posted 2017-11-02T17:59:52.973

Reputation: 541

1Nice idea! I really like it – aloisdg moving to codidact.com – 2017-11-04T11:55:24.447

3 bytes won in F# :) https://codegolf.stackexchange.com/a/147073/15214

– aloisdg moving to codidact.com – 2017-11-06T08:59:35.947

With currying you can win 1 byte! Try it online!

– aloisdg moving to codidact.com – 2017-11-06T12:35:34.617

1

SQLite, 118 bytes

SELECT MAX(x)FROM(SELECT X FROM A GROUP BY X HAVING COUNT(X)=1
INTERSECT
SELECT X FROM B GROUP BY X HAVING COUNT(X)=1)

Try it online!

First time in SQL, any help is welcome!

aloisdg moving to codidact.com

Posted 2017-11-02T17:59:52.973

Reputation: 1 767

1

Jq 1.5, 76 bytes

def m(n):[.[indices(.[])|select(length==n)[]]]|unique[];[map(m(1))|m(2)]|max

Expanded

def m(n): # function to emit elements of multiplicity n
  [
    .[                         # select elements with
         indices(.[])          # number of indices in the array
       | select(length==n)[]   # equal to specified multiplicity
    ]
  ] | unique[]                 # emit deduped values
;

[
    map(m(1))   # collect multiplicity 1 elements from each array
  | m(2)        # collect multiplicity 2 elements
] | max         # choose largest of these elements

Try it online!

Here is another solution which is the same length:

def u:[keys[]as$k|[.[$k]]-(.[:$k]+.[$k+1:])]|add;map(u)|.[0]-(.[0]-.[1])|max

Expanded

def u: # compute unique elements of input array
  [
      keys[] as $k                   # for each index k
    | [.[$k]] - (.[:$k]+.[$k+1:])    # subtract other elements from [ .[k] ]
  ]                                  # resulting in [] if .[k] is a duplicate
  | add                              # collect remaining [ .[k] ] arrays
;
  map(u)                             # discard duplicates from each input array
| .[0]-(.[0]-.[1])                   # find common elements using set difference
| max                                # kepp largest element

Try it online!

jq170727

Posted 2017-11-02T17:59:52.973

Reputation: 411

1

APL, 47 bytes

{1↑R[⍒R←((1={+/⍵=A}¨A)/A←⍺)∩(1={+/⍵=B}¨B)/B←⍵]}

Declares an anonymous function that takes two vectors, eliminates duplicate elements, then finds the biggest element in the intersection of the results.

A←⍺ and B←⍵ store the arguments passed to the function in A and B.

a=b returns a vector with 1 in each index in which a is equal to b. If a is a scalar (i.e. single quantity and not a vector) this returns a vector with 1 wherever the element in b is a and 0 when it is not. For example:

Input: 1=1 2 3
Output: 1 0 0

{+/⍵=A}: nested anonymous function; find the occurrences of the argument in vector A and add them up i.e. find the number of occurrences of the argument in A

1={+/⍵=A}¨A: apply the nested anonymous function to each element in A and find the ones that equal 1 i.e. unique elements

((1={+/⍵=A}¨A)/A←⍺): having found the location of the unique elements, select just these elements in the original vector (/ selects from the right argument elements whose locations correspond to 1 in the left argument)

R←((1={+/⍵=A}¨A)/A←⍺)∩(1={+/⍵=B}¨B)/B←⍵: repeat the process for the second argument; now that we have just the unique elements, find the intersection i.e. common elements and store this in vector R

R[⍒R]: access the elements of R in decreasing order

1↑R[⍒R]: take the first element of R when it's sorted in decreasing order

Test case:

Input: 17 29 39 29 29 39 18 18 {1↑R[⍒R←((1={+/⍵=A}¨A)/A←⍺)∩(1={+/⍵=B}¨B)/B←⍵]} 19 19 18 20 17 18
Output: 17

Arc676

Posted 2017-11-02T17:59:52.973

Reputation: 301

1

J, 30 bytes

[:>./@,[*([*+/"1(1=*/)+/)@(=/)

How it works:

I start with testing where the two list overlap by =/ (inserts equality test between all the members of the lists:

   a =. 1 3 4 6 6 9
   b =. 8 7 6 3 4 3
   ]c=. a =/ b 
0 0 0 0 0 0
0 0 0 1 0 1
0 0 0 0 1 0
0 0 1 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0

More than one 1 in the same column means that the number is not unique for the left argument (6 in this case); in the row - for the right argument (3)

Then I sum up all rows and all columns to find where are the duplicates:

   +/ c
0 0 2 1 1 1
   +/"1 c
0 2 1 1 1 0

I find the cartesian product of the above lists and set the members greater than 1 to 0.

    m =. (+/"1 c) (1=*/) +/c
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 0 0 0

I mask the equality matrix c with m to find the unique elements that are common to both lists and multiply the left argument by this.

]l =. a * m * c
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 4 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Then I flatten the list and find the max element:

 >./,l 
4

Try it online!

Galen Ivanov

Posted 2017-11-02T17:59:52.973

Reputation: 13 815

0

Octave, 57 56 bytes

@(x)max(intersect(cellfun(@(x){x(sum(x'==x)==1)},x){:}))

Anonymous function that takes as input a cell array of two numerical arrays.

Try it online!

Explanation

For each (cellfun(@(x)...)) of the two input arrays, this creates a matrix of pairwise equality comparisons between its entries (x.'==x); keeps (x(...)) only the entries for which the column sum is 1 (sum(...)==1); and packs the result in a cell ({...}). The intersection (intersect) of the two results ({:}) is computed, and the maximum (max(...)) is taken.

Luis Mendo

Posted 2017-11-02T17:59:52.973

Reputation: 87 464

0

Wolfram Language (Mathematica), 49 bytes

Last@Select[a=Join@##;Sort[#⋂#2],a~Count~#<3&]&

Try it online!

JungHwan Min

Posted 2017-11-02T17:59:52.973

Reputation: 13 290

0

Perl 5, 71 +2 (-ap) bytes

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

Try It Online

Nahuel Fouilleul

Posted 2017-11-02T17:59:52.973

Reputation: 5 582

0

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()

Roberto Graham

Posted 2017-11-02T17:59:52.973

Reputation: 1 305

0

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

RosLuP

Posted 2017-11-02T17:59:52.973

Reputation: 3 036

0

Ruby, 56 49 bytes

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

Try it online!

Reinstate Monica -- notmaynard

Posted 2017-11-02T17:59:52.973

Reputation: 1 053

0

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.

Progrock

Posted 2017-11-02T17:59:52.973

Reputation: 131

0

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

RosLuP

Posted 2017-11-02T17:59:52.973

Reputation: 3 036