Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
# Given 2 sorted arrays with unique values and arbitrary size, count the
# intersection values between them
array_a = [1, 2, 4, 5, 7, 8]
array_b = [0, 1, 3, 4, 6, 7, 9]
index_a = index_b = count = 0
loop do
break if array_a[index_a].nil? || array_b[index_b].nil?
check = array_a[index_a] <=> array_b[index_b]
count += 1 if check.zero?
index_a += 1 if check <= 0
index_b += 1 if check >= 0
end
puts count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment