Created
February 26, 2018 23:42
-
-
Save DNA/705302575d8ee2a13ab58388883a3e86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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