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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:47:03+00:00 2026-06-15T19:47:03+00:00

I have an array of questions where each question has a category_id and a

  • 0

I have an array of questions where each question has a category_id and a value.

I’d like to map these so that when the key (category_id) already exists in the hash, then the values are added together.

And, finally, I’d like to find the largest value in the hash:

h = Hash.new {|k, v| k[v] = 0}  

@test_session.answered_questions.each do |q|

  if h.key?(q.category_id)
     #add q.value to the value stored in the hash
  else
     h = { q.category_id => q.value } #insert the "q.category_id" as key and with value "q.value"
  end        

end

key_with_max_value = h.max_by { |k, v| v }[0] #find the highest value

@result.category = key_with_max_value
@result.score = h[key_with_max_value].value  

There is probably a lot better way to achieve this but I’m quite new to Ruby.

  • 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-15T19:47:04+00:00Added an answer on June 15, 2026 at 7:47 pm
    h = Hash.new(0)
    @test_session.answered_questions.each {|q| h[q.category_id] += q.value}
    @result.category, @result.score = h.max_by { |k, v| v }
    

    Each value in the hash will be initialized to zero with Hash.new(0) and since h.max_by returns the key value pair, you can directly assign them to your @result variable.

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

Sidebar

Related Questions

Noob question here! I have an array with hashes that looks like this: arr
I have a question about Quantifiers. Suppose that I have an array and I
I have a homework question that says: Problem 1: Given the array [ 22
I have my code that displays 92 selectors and each selector has a canvas(where
My app has Question models that has_many UserResponse models. I'd like to get the
I have a C library that has types like this: typedef struct { //
VISUAL C++ Question Hi, I have array of 3 elements and I want to
Very simple question... I have an array of pixels, how do I display them
I recently came across a question somewhere: Suppose you have an array of 1001
I know the question sounds silly, but consider this: I have an array of

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.