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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:30:29+00:00 2026-06-05T20:30:29+00:00

I would like to (quickly) determine if one array contains all the elements of

  • 0

I would like to (quickly) determine if one array contains all the elements of another array, taking into account that the arrays may have repeated elements.

Thus, I tried something like this:

alice = %w(a a a b)
bob = %w(a a b c d e)
alice & bob => ["a", "b"]
alice - bob => []

But what I would like is an operator that will let me determine that bob does not include all the elements of alice, because bob doesn’t have enough “a” characters.

  • 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-05T20:30:30+00:00Added an answer on June 5, 2026 at 8:30 pm
    alice.select{|x| alice.count(x) > bob.count(x)}
    

    Update
    Setting up a benchmark:

    require 'benchmark'
    
    def subset_multivalue?(a, b)
      bb = b.clone
      a.each do |e|
        i = bb.index(e)
        if i
          bb.delete_at(i)
        else
          return false
        end
      end
      return true
    end
    
    def subset_multivalue2?(a, b)
      a.find{|x| a.count(x) > b.count(x)}
    end
    
    def subset_multivalue3?(alice, bob)
      alice_counts = alice.each_with_object(Hash.new(0)) { |o, h| h[o] += 1 }
      bob_counts = bob.each_with_object(Hash.new(0)) { |o, h| h[o] += 1 }
    
      alice_counts.all? do |k, v|
        bob_counts.has_key?(k) && bob_counts[k] >= v
      end
    end
    
    alice = %w(a a a b)
    bob = %w(a a b c d e)
    
    Benchmark.bm do |x|
      x.report("dave:") do
        1000000.times do
          subset_multivalue?(alice, bob)
        end
      end
    
      x.report("me:") do
        1000000.times do
          subset_multivalue2?(alice,bob)
        end
      end
    
      x.report("andrew:") do
        1000000.times do
          subset_multivalue3?(alice,bob)
        end
      end
    end
    

    Results:

           user     system      total        real
    dave: 15.054000   0.000000  15.054000 ( 15.108864)
    me: 11.529000   0.031000  11.560000 ( 11.689669)
    andrew: 65.036000   0.047000  65.083000 ( 67.463859)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am starting an application that I would like to build quickly and will
I would like to have a mapping to quickly quit all buffers, for use
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to temporarily impersonate a domain user account to read in a
I would like to quickly retrieve the median value from a boost multi_index container
Sometimes I would like to quickly see the IL representation of my code snippets
I would like to time how quickly the latency is of a system by
I would like to know whether WinInet or WinHttp will get webpage contents quickly
I would like to define a large bitfield for the purpose of quickly monitoring
On my homepage the banner transitions are going very quickly- I would like to

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.