-
-
Save pvin/eb793f0e333da053e84d9a7364f3a5e7 to your computer and use it in GitHub Desktop.
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
#Usual way | |
unique = [] | |
words.each { |word| unique << word unless unique.include?(word) } | |
#What we really need is a collection that doesn’t allow duplicates but does feature very fast and easy answers to the “is this | |
#object in there?” question. The punch line is that we need a set. Fortunately, Ruby comes with a perfectly serviceable, if | |
#sometimes forgotten, | |
#Set class: | |
require 'set' | |
word_set = Set.new( words ) | |
#methods | |
s1.subset? s2 | |
s2.subset? s1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment