I need a collection that is like a set. Basically I'm scanning a long string and adding words to the collection but I want to be able to detect when there are duplicates.
If sets aren't available, what's the most efficient way of doing this in Ruby? Brownie points for example code.
What is a Ruby set? A set is a class that stores items like an array… But with some special attributes that make it 10x faster in specific situations!
A Quick Intro to Ruby's Set (3 Part Series) All objects in a Set are guaranteed unique. Objects in a Set are not ordered. Sets are built on top of Hashes for super-fast object lookup.
%w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.
unshift will add a new item to the beginning of an array. With insert you can add a new element to an array at any position.
There is a Set class in ruby. You can use it like so:
require 'set' set = Set.new string = "a very very long string" string.scan(/\w+/).each do |word| unless set.add?( word ) # logic here for the duplicates end end
Although, I'm wondering if you would want to count the instances in that case the following example would be better:
instances = Hash.new { |h, k| h[k] = 0 } string.scan(/\w+/).each do |word| instances[word] += 1 end
From the documentation:
a = [ "a", "a", "b", "b", "c" ] a.uniq #gets you ["a", "b", "c"] a.uniq.uniq! #gets you nil (no duplicates :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With