Code Golf
counting add tag
DJMcMayhem
Given two lists, **A** and **B**, return the first element of **A** that appears the fewest times in **B**.

For example, 

```
A = [1, 2, 3, 4, 5, 6]
B = [3, 1, 4, 1, 5, 9]
```

The elements '2' and '6' both appear **0** times in **B**. Because '2' occurs first, your program/function should return 2. (note that '9' appears in B but not in A.)

**B** may be empty, but **A** will always have at least 1 element.

More examples:

```
A = [1, 2, 3, 4]
B = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
-> 3

A = [1, 2, 3, 4]
B = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]
-> 4

A = [4, 8, 15, 16, 23, 42]
B = []
-> 4

A = [5]
B = [1, 2, 3, 4, 5, 6, 7]
-> 5

A = [10]
B = [1, 2, 3]
-> 10

A = [6, 5, 4, 3, 2, 1]
B = [5, 2, 3]
-> 6
```
Top Answer Python 2
xnor
# 29 bytes

```
lambda a,b:min(a,key=b.count)
```

[Try it online!][TIO-k7dy5gfl]

[TIO-k7dy5gfl]: https://tio.run/##bY7BCoMwEETvfsUetSylica2gl9iPcRWMVSjlEjx69NNsCVIYQ@782aYnVfTT5rbrrzZQY7NQ4LEphiVjiU@27Vsjvdp0Sax714NLbBifiltoIsPSs@LiZPEVgyBI6QIGYJAyGuEii7mBea1ax0FNmcIQ/v9n/k3/DvpxqOKYhdi1MNyQk7lLkZI7LvcgwhnV3EKGN25h5k3ksQcFRv9AA "Python 2 – Try It Online"

Conveniently, Python chooses the earliest element in case of a tie.
Answer #2 SQL (Postgres)
Jack Douglas
# 148 bytes

```
select v from(select*from unnest((select v from a))with ordinality a(v,o))w natural left join b group by v order by count(b.v) desc,min(o) limit 1;
```

<>https://dbfiddle.uk/?rdbms=postgres_12&fiddle=6c90524b802f73aacb2f6d372a310e18&hide=15

Explanations etc.

The order of elements in B is immaterial so it is represented as a regular table but A is an array as the order matters in that input.
Answer #3 APL (Dyalog Extended)
Adám
# 6 bytes
Anonymous tacit infix function taking `A` as left argument and `B` as right argument.
```
⊃⍧⍋⍛⊇⊣
```
[Try it online!][TIO-k7cy1b6e]

`⊃` the first of

`⍧` the **C**ount-`A`-**I**n-`B`

`⍋` ascending ordered

`⍛` then

`⊇` used to reorder

`⊣` `A`

[TIO-k7cy1b6e]: https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1FX86Pe5Y96ux/1zn7U1f6oa/H/tEdtEx719oFl1jzq3XJovfGjtomP@qYGBzkDyRAPz@D/jkA1hgpGCsYKJgqmCmZcTkC@sYIhkGcI5FtyOSqkKThxcSEpAyuBaUGicSuFQCMwNAaLQ5U@6p2rAFJuomChYGiqYGimYASUNVJQAEmU5xdlFyvkp6XlZOal6igklZYohHj6K2QkAgVzUhTKUouKM/PzwGY4gT26BmIewhU6pihuBXpPwRzZlTqGBggFyBJmQLUmQD1GCoZgBaYIBQA "APL (Dyalog Extended) – Try It Online"
Answer #4 Kotlin
ajc2
# 29 bytes
-6 by changing `sortedBy` to `minBy`.

```
{b->minBy{b.count{v->v==it}}}
    
{b->                           // A is this, B is arg
    minBy{                     // select minimum value of A by
          b.count{v->v==it}}}  // # of occurrences in B
```

`minBy` always appears to take the earlier item in the event of a tie.

[Try it online!][TIO-k845wpk5]

[TIO-k845wpk5]: https://tio.run/##Xc5LCsIwFIXheVdxhgmkhfoCxURwJgiCO0jEQrBNpb0tSMjaY6xQxDs6g4@f@2ipti6OukZlu56uurv3tMPZ9rQ/OVIFmydHrpDGATJ6k6vGuuPLm@LWDo78mKtRSkshhFgNDo22jnH4DOk@fQ2JOrUu1VRjpcBCYCmwElgLbPgszZ9MppxYOcntVz4764j9fM20MJxnIb4B "Kotlin – Try It Online"
Answer #5 05AB1E
liek
# 5 bytes
Input should be taken in the order `B, A` instead.

    s¢Wkè

[Try it online!][TIO-k7ddy8ki]

[TIO-k7ddy8ki]: https://tio.run/##yy9OTMpM/f@/@NCi8OzDK/7/jzbWUTDUUTABk6Y6CpaxXNFAlpGOgjFYFChkFgsA "05AB1E – Try It Online"
# Explanation
```
s      Swap the arguments so that the order becomes A, B
 ¢     Count how many times the items in A occur in B
  W    Push the minimum of these occurences
       without popping the original list
   k   Index into the original list, yielding the first
       index satisfying that
    è  Index into the same index of the original list
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.