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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:35:50+00:00 2026-06-01T02:35:50+00:00

My goal is to convert a into b: a = [[a,b], [d, c], [a,

  • 0

My goal is to convert a into b:

a = [["a","b"], ["d", "c"], ["a", "o"], ["d", "g"], ["c", "a"]]
b = [[["a","b"], ["a", "o"]], ["c", "a"], [["d", "c"], ["d", "g"]]

They are grouped by the first element in each nested array. So far I have:

def letter_frequency(c)
  d = Hash.new(0)
  c.each do |v|
    d[v] += 1
  end
  d.each do |k, v|
  end
  end

def separate_arrays(arry)
  arry2 = []
  arry3 = []
  big_arry = []
  y = 0
 while y < arry.length 
    arry2.push(arry[y][0])
    arry3.push(arry[y][1])         
    y += 1
 end
  freq = letter_frequency(arry2)
  front = arry.slice!(0..(freq["a"] - 1))
end

separate_arrays(a)

Not only does this seem like overkill, but there are now guarantees that “a” will be a legit Hash key, so the last part doesn’t work. Thanks for any help.

  • 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-01T02:35:52+00:00Added an answer on June 1, 2026 at 2:35 am

    You can try to do something like this:

    a.group_by(&:first).values.map {|e| e.length > 1 ? e : e.flatten}
    # => [[["a", "b"], ["a", "o"]], [["d", "c"], ["d", "g"]], ["c", "a"]]
    

    I use the following methods:

    Enumerable#group_by (by first element of an array, like in your question):

    Returns a hash, which keys are evaluated result from the block, and
    values are arrays of elements in enum corresponding to the key.

    Hash#values:

    Returns a new array populated with the values from hsh. See also Hash#keys.

    Enumerable#map (required because you don’t want to get nested array when there are only one match, like for c letter):

    Returns a new array with the results of running block once for every element in enum.

    Enumerable#flatten:

    Returns a new array that is a one-dimensional flattening of this array
    (recursively). That is, for every element that is an array, extract
    its elements into the new array. If the optional level argument
    determines the level of recursion to flatten

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

Sidebar

Related Questions

My goal is to convert list into array using split method then remove element(depends)
goal: I have the string 1234432144 I want to only replace the first 2
My goal is to convert an RGB pixel into CIELab color space for some
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
My goal is to convert a PDF into an image (specifically in TIFF). There
My goal is to convert a timestamp from MySQL into a JavaScript Date object
I'm testing .htaccess file on the server. The goal is to convert www.mysite.com/index.php?q=help into
I need to convert integer value into char array on bit layer. Let's say
My Goal I would like to have a main processing thread (non GUI), and
I have intellectual-property coded into .net 2.0 fully-trusted assemblies (.exe + DLLs) on an

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.