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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:33:35+00:00 2026-06-07T03:33:35+00:00

I have the following arrays: A = cheddar.split(//) # [c, h, e, d, d,

  • 0

I have the following arrays:

A = "cheddar".split(//)  # ["c", "h", "e", "d", "d", "a", "r"]
B = "cheddaar".split(//) # ["c", "h", "e", "d", "d", "a", "a", "r"]

The A array is a subset of the B array. If the A array had another “d” element, it wouldn’t be a subset.

I want to compare and find if one is a subset of the other even if they have duplicates. The A – B or the A & B doesn’t capture the duplicates it just compares them and find them matched. So I wrote the following which it captures the duplicates:

B.each do |letter|
    A.delete_at(A.index(letter)) rescue ""
end

p A.empty?

Is this the best way or can it be optimized?

  • 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-07T03:33:37+00:00Added an answer on June 7, 2026 at 3:33 am

    A similar question was posted a few weeks ago and I got the accepted answer with something like:

    def is_subset?(a,b)
      !a.find{|x| a.count(x) > b.count(x)}
    end
    

    Benchmark Update

    require 'benchmark'
    
    def random_char
      ('a'..'z').to_a.sample
    end
    
    A = 8.times.map{random_char}
    B = 8.times.map{random_char}
    
    def ary_subset?(a,b) # true iff a is subset of b
      a_counts = a.reduce(Hash.new(0)) { |m,v| m[v] += 1; m }
      b_counts = b.reduce(Hash.new(0)) { |m,v| m[v] += 1; m }
      a_counts.all? { |a_key,a_ct| a_ct <= b_counts[a_key] }
    end
    
    Benchmark.bm do |x|
      x.report('me')     {100000.times{is_subset?(A,B)}}
      x.report('dbenhur'){100000.times{ary_subset?(A,B)}}
    end
    

           user     system      total        real
    me  0.375000   0.000000   0.375000 (  0.384022)
    dbenhur  2.558000   0.000000   2.558000 (  2.550146)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following two arrays and the code to find array_diff: $obs_ws = array(you,
I have the following array: $array = array(1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,1,0,1,0,0,1,0,1); I want split it up into
I have following two arrays. I want the difference between these two arrays. That
I have the following three arrays and need to create a new two-dimensional array
If I have the following arrays arry1 = array( 101 => array( 'title1' =>
I have the following two arrays: Array ( [Jonah] => 27 [Bianca] => 32
I have the following array or arrays: $bigArray = array( array( id = 3,
I have following arrays : $t[0] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4)); $t[1] = array('two'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4)); $t[2] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
I have the following arrays: array = [ [link_text1, link1],[link_text2, link2], ... ] array
i have foreach, which generate following arrays: ==== array 1 ==== array 0 =>

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.