How to find and return a duplicate value in array

arr is array of strings:

["hello", "world", "stack", "overflow", "hello", "again"]

What would be an easy and elegant way to check if arr has duplicates, and if so, return one of them (no matter which)?

Examples:

["A", "B", "C", "B", "A"]    # => "A" or "B"
["A", "B", "C"]              # => nil

23 Answers
23

Leave a Comment