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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:48:24+00:00 2026-05-17T06:48:24+00:00

The canonical Array difference example in Ruby is: [ 1, 1, 2, 2, 3,

  • 0

The canonical Array difference example in Ruby is:

[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]

What’s the best way to get the following behavior instead?

[ 1, 1, 2, 2, 3, 3, 4, 5 ].subtract_once([ 1, 2, 4 ])  #=>  [ 1, 2, 3, 3, 5 ]

That is, only the first instance of each matching item in the second array is removed from the first array.

  • 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-05-17T06:48:25+00:00Added an answer on May 17, 2026 at 6:48 am

    Subtract values as many times as they appear in the other array, or any Enumerable:

    class Array
      # Subtract each passed value once:
      #   %w(1 2 3 1).subtract_once %w(1 1 2) # => ["3"]
      #   [ 1, 1, 2, 2, 3, 3, 4, 5 ].subtract_once([ 1, 2, 4 ]) => [1, 2, 3, 3, 5]
      # Time complexity of O(n + m)
      def subtract_once(values)
        counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
        reject { |e| counts[e] -= 1 unless counts[e].zero? }
      end
    

    Subtract each unique value once:

    require 'set'
    class Array
      # Subtract each unique value once:
      #   %w(1 2 2).subtract_once_uniq %w(1 2 2) # => [2]
      # Time complexity of O((n + m) * log m)
      def subtract_once_uniq(values)
        # note that set is implemented 
        values_set = Set.new values.to_a 
        reject { |e| values_set.delete(e) if values_set.include?(e) }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the canonical way to get an empty array in Scala? new Array[String](0)
Is there a canonical best-way-to-do-this for the following situation? I have a block of
Is there a canonical way to randomize an array in Objective-C?
I'm wondering what's the canonical way in Ruby to create custom setter and getter
What's the best/canonical way to define a function with optional named arguments? To make
I'm wondering if there is a more canonical way to do this in ruby
Does anyone know where I can get a canonical XML document that uses every
Is there a canonical way of doing that? I've been using head -n |
Is there an easy way in Ruby to find a canonical file path out
The canonical example for Twitter Bootstrap's popover feature is sort of a tooltip on

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.