Possible Duplicate:
Ruby Hash with Duplicate Keys?
I defined a hash:
sorted_words = Hash.new(0)
words.each { |word| sorted_words[word]=word.downcase.chars.sort{ |a, b| a.casecmp(b) }.join
Hash does not allow the duplicate keys so if I have keys like cream, scream, scream, it only considers the first two. But I want to also have the third key saved in my Hash with its appropriate value.
This is for an anagram. After the above code I create another hash and, depending on my values from the code, I create multiple arrays with each array having the anagram string.
What could be a solution for such a situation?
Based on the comment you’re looking for an anagram maker, here’s the basis for such a beast:
Given a set of words, this will create a hash, where each key is the sorted list of the letters in the word. The value associated with that key is a set of words with the same letters.
Because the value is a set, only unique words are stored.
Once that hash is populated, you’ll have a dictionary you can use to quickly look up other words. Take a word, break it down the same way the keys are broken down, and use that as the key in a look-up:
outputs:
If duplicate (and redundant) words are needed: