Interview Question


Write an algorithm that tells me if 2 and only two numbers

  in a 1x3 array are the same.

Interview Answer

1 Answer


In ruby: def find_it(arr) a = arr.each do |b| a[b] += 1 end a.each do |k, v| return true if v == 2 end return false end That will find two and only two matches in an arbitrarily long array. For extra credit, how can you optimize it for an array of only 3 elements?

Renski on Apr 20, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.