Friday, 17 May 2019

Find the uniq elements in an array without using ruby default method?

a = [1, 2, 3, 4, 5, 6, 4, 3, 2, 1, 4, 5, 4, 34, 76]

Solution:

b = Hash.new 0

a.each do |k|
  b[k] += 1
end

b.keys

Output:
=> [1, 2, 3, 4, 5, 6, 34, 76]

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...