or
DJMcMayhem
counting
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

Jack Douglas
# [SQL (Postgres)](https://www.postgresql.org/docs/12/sql.html), 148 bytes

sql
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.
# [APL (Dyalog Extended)], 6 bytes
Anonymous tacit infix function taking A as left argument and B as right argument.
apl
⊃⍧⍋⍛⊇⊣

[Try it online!][TIO-k7cy1b6e]

⊃ the first of

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

⍋ ascending ordered

⍛ then

⊇ used to reorder

⊣ A

[APL (Dyalog Extended)]: https://github.com/abrudz/dyalog-apl-extended
[TIO-k7cy1b6e]: https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1FX86Pe5Y96ux/1zn7U1f6oa/H/tEdtEx719oFl1jzq3XJovfGjtomP@qYGBzkDyRAPz@D/jkA1hgpGCsYKJgqmCmZcTkC@sYIhkGcI5FtyOSqkKThxcSEpAyuBaUGicSuFQCMwNAaLQ5U@6p2rAFJuomChYGiqYGimYASUNVJQAEmU5xdlFyvkp6XlZOal6igklZYohHj6K2QkAgVzUhTKUouKM/PzwGY4gT26BmIewhU6pihuBXpPwRzZlTqGBggFyBJmQLUmQD1GCoZgBaYIBQA "APL (Dyalog Extended) – Try It Online"
xnor
# [Python], 29 bytes

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

[Try it online!][TIO-k7dy5gfl]

[Python]: https://docs.python.org/2/
[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.
liek
# [05AB1E], 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

ajc2
# [Kotlin], 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]

[Kotlin]: https://kotlinlang.org
[TIO-k845wpk5]: https://tio.run/##Xc5LCsIwFIXheVdxhgmkhfoCxURwJgiCO0jEQrBNpb0tSMjaY6xQxDs6g4@f@2ipti6OukZlu56uurv3tMPZ9rQ/OVIFmydHrpDGATJ6k6vGuuPLm@LWDo78mKtRSkshhFgNDo22jnH4DOk@fQ2JOrUu1VRjpcBCYCmwElgLbPgszZ9MppxYOcntVz4764j9fM20MJxnIb4B "Kotlin – Try It Online"
First rarest element
Jack Douglas
obviously we don't want to be adding burdens but it sounds like he may prefer not to be doing it on his own
Jack Douglas
I'm not sure how heavy his hosting needs are compared to db<>fiddle, but that's something we could consider offering
Jack Douglas
i wonder if he would appreciate the offer of teaming up
Jack Douglas
https://chat.stackexchange.com/transcript/message/53429076#53429076
that's helpful to know, thanks
We pay pretty much all of his expenses.
https://chat.stackexchange.com/transcript/message/53428659#53428659
Jack Douglas
From the [TIO home page](https://tio.run/#): "TIO is getting more and more traffic, so additional arenas will be required. Also, server-side permalinks will eventually require a separate storage. With your help, I hope to ensure a smooth operation of all TIO services.", so I imagine it'd be safer to run our own instances
I've tried to get in touch with Dennis on [the TIO forum](https://gitter.im/tryitonline/Forum), but no reply so far and it seems quiet in there. I think putting the effort in to get TIO working here will be well worth it as it'll help other communities too, so we will definitely give it a go. Can you point me at a good place to start of the unofficial api?
It has an (unofficial) API, which several people have written code for in various languages (Dyalog uses two separate interfaces, one in APL and one in JS).
Jack Douglas
I haven't looked at the code at all
does TIO have an api we could use for that or are you suggesting some sort of scraping?
We're also working on a replacement for the existing https:/tryapl.org which offloads computations to TIO as backend. You can see the WIP prototype [here](https://problems.tryapl.org/tryapl/).
Yes, but I actually meant using the existing TIO. Dyalog host [a competition](https://www.dyalogaplcompetition.com/) where phase I includes instant checking of the submissions, but the actual execution is offloaded to TIO. My idea was simply that you could recognise <>https://tio.run/…, decode and display the fields, and insert a [run] button which would update the Output and Debug fields.
Jack Douglas
to you happen to know the creator of TIO?
Jack Douglas
I imagine it would be possible to separate the TIO bits that are inherently vulnerable onto one or more stateless VMs like the db<>fiddle backends
yes I think so — I looked into it briefly before. It's (obviously?) asking to get hacked running a server like that but the same is true for the db<>fiddle backends, so we already run them on dedicated hardware quarantined from all other VMs and could simply add any TIO server onto that platform.
Correct
@Jack The dbfiddle is neat. Any chance of getting TIO support?
I think it is clear that B's order doesn't matter.
@DJMcMayhem Am I right that the order of elements in input B doesn't matter?