Thursday, February 11, 2021

Coding Interview Question

        # 0 1 2 3 4 5 6

input = [1,2,4,2,5,3,5]


def duplicates(input)

  hash = Hash.new(0)

  output = Hash.new([])

  

  input.each do |n|

    hash[n] += 1

  end

  

  hash.each do |k,v|

    if v > 1

      output[k] = []

      input.each_with_index do |e, i|

        if e == k

          output[k] << i

        end

      end

    end

  end

  

  output

end


p duplicates(input)