Find Duplicate

Find the first duplicate element that appears in an array using a hash set for efficient lookup.

Code

Algorithms
require 'set'
seen = Set.new
dup = arr.find { |x| seen.include?(x) || !seen.add(x).nil? && false }
return dup

Parameters

Array to check

Server

More Ruby Snippets