Last active
January 19, 2019 15:03
-
-
Save mrzasa/b8436b36d4db70fffc3e14eca2dc2ff5 to your computer and use it in GitHub Desktop.
Find intersection of arrays
This file contains 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
# arrays | |
result = [] | |
# initial data | |
values = arrays.map{|a| a.next} | |
while true | |
maximal_value = values.max | |
for i 0...values.size do | |
while values[i] < maximal_value do | |
values[i] = arrays[i].next | |
break unless values[i] # assuming that #next returns nil if there is no more elements | |
end | |
end | |
break if values.any{|v| v.nil? } # one of the arrays is finished | |
if values.all?{|v| v == maximal_value} | |
result << maximal_value | |
end | |
end | |
result |
stulentsev
commented
Jan 19, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment