So, I have an array
['a', 42, 'b', 42, 'a']
I want to alert users to the fact that 42 and 'a' appear in it twice.
some_function(['a', 42, 'b', 42, 'a'])
should return
['a', 42]
Write some code to extract the duplicates from an array.
- It should accept an array as input.
- The array can have multiple types of object.
- Duplication should be defined as per the language's standard equality function (examples are from ruby).
- It outputs an array of the duplicated items.
- The order of the returned items is irrelevant.
- Duplicates should only appear in the returned array once.
- Golf rules, do it in the smallest number of bytes.
Test cases
[1,2,3] => []
[1,2,3,2,1] => [1,2]
[1.0, 2, 2.0, 1.0] => [1, 2] or [1.0, 2.0] or [1, 2.0] or [1.0, 2]
[1, '1', '1.0'] => []
[nil, nil, false, false, true, true] => [nil, false, true]
[1, 1, 1, 2, 2]
, so that one element is repeated more than once \$\endgroup\$[1.0, 2, 2.0, 1.0]
could be interpreted as having only one duplicate:1.0
, if one considers the integer2
and which might be a non-integer2.0
to be not equal. Since this is intended to allow for different types. \$\endgroup\$Object.equals
and==
, JS have===
and==
, Python have==
andis
, and it's 100% opinion-based which one is more "standard"). \$\endgroup\$