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 7514905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:04+00:00 2026-05-30T00:38:04+00:00

For example Given an array: array = [[:a,:b],[:a,:c],[:c,:b]] Return the following hash: hash =

  • 0

For example

Given an array:

array = [[:a,:b],[:a,:c],[:c,:b]]

Return the following hash:

hash = { :a => [:b,:c] , :c => [:b] }

hash = Hash[array] overwrites previous associations, producing:

hash = { :a => :c , :c => :b }
  • 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-05-30T00:38:04+00:00Added an answer on May 30, 2026 at 12:38 am

    Using functional baby steps:

    irb:01.0> array = [[:a,:b],[:a,:c],[:c,:b]]
    #=> [[:a, :b], [:a, :c], [:c, :b]]
    
    irb:02.0> array.group_by(&:first)
    #=> {:a=>[[:a, :b], [:a, :c]], :c=>[[:c, :b]]}
    
    irb:03.0> array.group_by(&:first).map{ |k,a| [k,a.map(&:last)] }
    #=> [[:a, [:b, :c]], [:c, [:b]]]
    
    irb:04.0> Hash[ array.group_by(&:first).map{ |k,a| [k,a.map(&:last)] } ]
    #=> {:a=>[:b, :c], :c=>[:b]}
    

    Using imperative style programming:

    irb:10.0> h = Hash.new{ |h,k| h[k]=[] }
    #=> {}
    
    irb:11.0> array.each{ |k,v| h[k] << v }
    #=> [[:a, :b], [:a, :c], [:c, :b]]
    
    irb:12.0> h
    #=> {:a=>[:b, :c], :c=>[:b]}
    

    As an imperative one-liner:

    irb:13.0> h = Hash.new{ |h,k| h[k]=[] }.tap{ |h| array.each{ |k,v| h[k] << v } }
    #=> {:a=>[:b, :c], :c=>[:b]}
    

    Or using everyone’s favorite inject:

    irb:14.0> array.inject(Hash.new{ |h,k| h[k]=[] }){ |h,(k,v)| h[k] << v; h }
    #=> {:a=>[:b, :c], :c=>[:b]}
    

    If you really want to have single values not collided as an array, you can either un-array them as a post-processing step, or use a different hash accumulation strategy that only creates an array upon collision. Alternatively, wrap your head around this:

    irb:17.0> hashes = array.map{ |pair| Hash[*pair] } # merge many mini hashes
    #=> [{:a=>:b}, {:a=>:c}, {:c=>:b}]
    
    irb:18.0> hashes.inject{ |h1,h2| h1.merge(h2){ |*a| a[1,2] } }
    #=> {:a=>[:b, :c], :c=>:b}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';
I have the following script that dumps a given array's contents function dump(obj) {
Taking a dictionary as an example, let's say I've got the following array to
Going from the example given here... http://ericswann.org/blog/archive/2009/04/06/linq-to-sql-datacontext-provider-revisited.aspx I'm trying to use the datacontext between
In most languages like C# for example given a string you can test (boolean)
I'm trying to run through an example given for the C++ Xerces XML library
I am trying to use Class::DBI with Catalyst::Plugin::Authentication::Store::DBIC. The example given on CPAN does
In one of the example servers given at golang.org: package main import ( flag
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active

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.