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

  • Home
  • SEARCH
  • 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 8788177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:06:00+00:00 2026-06-13T22:06:00+00:00

The problem would be easy to solve with a manual loop and a result

  • 0

The problem would be easy to solve with a manual loop and a result array, which you add onto as you go. But I’m looking for a more ruby-esque solution, probably something that uses inject or select. Here’s the problem:

arr_of_hashes = [
  {id: 1, val: "blah1"},
  {id: 1, val: "blah2"},
  {id: 1, val: "blah3"},
  {id: 2, val: "blah4"},
  {id: 2, val: "blah5"},
  {id: 3, val: "blah6"},
  {id: 3, val: "blah7"},
  {id: 3, val: "blah8"},
  {id: 3, val: "blah9"}
]

“Groups” are defined by the “id” field in the hashes. We are guaranteed each group has at least two items. We want to return an array containing the 2nd item of each group:

output_should_be = [
  {id: 1, val: "blah2"},
  {id: 2, val: "blah5"},
  {id: 3, val: "blah7"}
]
  • 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-13T22:06:01+00:00Added an answer on June 13, 2026 at 10:06 pm

    “I’m looking for a more ruby-esque solution”. I guess you mean functional:

    arr_of_hashes.chunk { |h| h[:id] }.map { |id, hs| hs[1] }
    #=> [{:id=>1, :val=>"blah2"}, {:id=>2, :val=>"blah5"}, {:id=>3, :val=>"blah7"}]
    

    Use group_by if elements are not pre-ordered by id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem that would seem to be easy to solve, but
I'm facing a problem that I assumed would be very easy to solve but...
I'd figure this would be an easy problem to solve, but I've been googling
This particular problem is easy to solve, but I'm not so sure that the
This problem is probably very easy to solve but somehow I cannot find a
I think I have a pretty easy problem to solve, but I have been
I know this is a quite easy problem but I just want to solve
I am trying to solve a fairly easy problem but can't quite figure out
I suppose similar problem would have been discussed here, but I couldn't find it.
I have an interesting genetics problem that I would like to solve in native

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.