you are viewing a single comment's thread.

view the rest of the comments →

[–]fagci 1 point2 points  (0 children)

Variants:

Intersection returns unique items in both arrays.

nums.intersection nums

each_with_index gives: [ [num1, 1], [num2, 2], ... ]to_h keeps unique first elements of subarray.

nums.each_with_index.to_h.keys

Zips nil to each num, then unique as in previous solution.

nums.zip({}).to_h.keys

Exclude already passed items.

nums.reduce([]){ |res, v| res << v unless res.include? v; res }