Created
August 23, 2012 21:35
-
-
Save jumph4x/3442159 to your computer and use it in GitHub Desktop.
Subset Check
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
def is_subset?(a,b) | |
return true if b and b.is_a? Array and b.empty? # O(1) | |
hash = {} # O(1) | |
a.each{|v| hash[v] = true} # O(n) outer, O(1) inner, O(n) total | |
b.map{|v| hash[v] }.uniq == [true] # O(n) outer, O(1) inner, O(n) total | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment