Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:43:09+00:00 2026-06-12T13:43:09+00:00

Possible Duplicate: Ruby Hash with Duplicate Keys? I defined a hash: sorted_words = Hash.new(0)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:43:10+00:00Added an answer on June 12, 2026 at 1:43 pm

    Based on the comment you’re looking for an anagram maker, here’s the basis for such a beast:

    require 'pp'
    require 'set'
    
    words = %w[cream scream scream creams]
    
    hash = Hash.new{ |h,k| h[k] = Set.new }
    
    words.each do |w|
      hash[w.downcase.split('').sort] << w.downcase
    end
    
    pp hash
    
    => {["a", "c", "e", "m", "r"]=>#<Set: {"cream"}>,
     ["a", "c", "e", "m", "r", "s"]=>#<Set: {"scream", "creams"}>}
    

    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:

    puts hash['creams'.downcase.split('').sort].to_a.join(', ')
    

    outputs:

    scream, creams
    

    If duplicate (and redundant) words are needed:

    require 'pp'
    require 'set'
    
    words = %w[cream creams scream scream]
    
    hash = Hash.new{ |h,k| h[k] = [] }
    
    words.each do |w|
      hash[w.downcase.split('').sort] << w.downcase
    end
    
    pp hash
    
    => {["a", "c", "e", "m", "r"]=>["cream"],
     ["a", "c", "e", "m", "r", "s"]=>["creams", "scream", "scream"]}
    
    puts hash['creams'.downcase.split('').sort].to_a.sort.join(', ')
    
    => creams, scream, scream
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a hash in Ruby that allows duplicate keys? I'm
Possible Duplicate: Ruby syntax question: Rational(a, b) and Rational.new!(a, b) I'm in the process
Possible Duplicate: Sorting a hash in Ruby by its value first then its key.
Possible Duplicate: Checking if a variable is defined in Ruby using Tilt's template render
Possible Duplicate: Ruby - What is the difference between map, each and collect? I
Possible Duplicate: Sort strings and numbers in Ruby I have an array of place
Possible Duplicate: What does ||= mean in Ruby? I'm new to ruby and I
Possible Duplicate: What does !! mean in ruby? Hi, I'm new to Ruby and
Possible Duplicate: Sorting an array in descending order in Ruby I want to sort
Possible Duplicate: Ruby On Rails 3 and Webrick issue $ rails server /Users/Vineeth/.rvm/gems/ruby-1.9.3-p194/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require':

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.