Skip to content

Instantly share code, notes, and snippets.

  • Save pvin/eb793f0e333da053e84d9a7364f3a5e7 to your computer and use it in GitHub Desktop.
Save pvin/eb793f0e333da053e84d9a7364f3a5e7 to your computer and use it in GitHub Desktop.
#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