8
Input
Take a list of values xi each paired with a key yi.
[(x1, y1), (x2, y2), ...]
Output
Return a list L containing only values from the set {xi}.
- The length of L must be equal to the number of unique keys k in the set {yi}.
- For each unique key k there must be a value from {xi} that has key k.
Details
- Standard loopholes disallowed.
- You can assume all values in the input will be nonnegative integers.
- There may be duplicate values and keys.
- You can assume there is at least one value/key pair in the input.
- If you prefer to take two lists of equal length as input (one for values, one for keys) that is fine.
- You may not take any other input.
- The order of the list you output does not matter.
- The xi you choose for each key does not matter.
For example, with input [[0, 0], [1, 3], [2, 3]]
you can return either [0, 1]
or [0, 2]
or any permutation of these.
Examples
[[1, 2], [3, 2], [3, 0]] -> [1, 3] or [3, 3]
[[7, 2], [7, 0], [7, 1]] -> [7, 7, 7]
[[4, 0], [4, 0], [9, 1], [5, 2]] -> [4, 9, 5]
[[9, 1], [99, 10], [5, 5], [0, 3]] -> [9, 99, 5, 0]
Fewest bytes wins.
Can I take input in the form
key value key value key value ...
? – wastl – 2018-06-06T15:10:42.773@wastl Yes you can – dylnan – 2018-06-06T15:15:12.147
What if your language doesn't support Maps containing the same
key
s? Can we take two arrays askeys
andvalues
as input? Or create our own custom Map that does take multiple values as input (or perhaps a list of key-value pairs)? – Kevin Cruijssen – 2018-06-06T15:15:23.6701@KevinCruijssen
If you prefer to take two lists of equal length as input that is fine.
Is this what you mean? I don't know what you mean about "Maps". – dylnan – 2018-06-06T15:17:45.717@dylnan Ah, that was indeed what I meant, thanks. Read past it. And "maps" is the term for key-value pairs in Java, not sure if it's called differently in other languages. – Kevin Cruijssen – 2018-06-06T15:19:02.163
@KevinCruijssen "maps" is how I learnt it, some languages like Perl call them hashes (short for hash maps I guess), and the currently popular term seems to be dictionaries or dicts. – sundar - Reinstate Monica – 2018-06-06T21:28:33.140