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

  • Home
  • SEARCH
  • 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 605045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:06:13+00:00 2026-05-13T17:06:13+00:00

I have two array I need to merge, and using the Union (|) operator

  • 0

I have two array I need to merge, and using the Union (|) operator is PAINFULLY slow.. are there any other ways to accomplish an array merge?

Also, the arrays are filled with objects, not strings.

An Example of the objects within the array

#<Article 
 id: 1, 
 xml_document_id: 1, 
 source: "<article><domain>events.waikato.ac</domain><excerpt...", 
 created_at: "2010-02-11 01:32:46", 
 updated_at: "2010-02-11 01:41:28"
>

Where source is a short piece of XML.

EDIT

Sorry! By ‘merge’ I mean I need to not insert duplicates.

A => [1, 2, 3, 4, 5]
B => [3, 4, 5, 6, 7]
A.magic_merge(B) #=> [1, 2, 3, 4, 5, 6, 7]

Understanding that the integers are actually Article objects, and the Union operator appears to take forever

  • 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-13T17:06:14+00:00Added an answer on May 13, 2026 at 5:06 pm

    Here’s a script which benchmarks two merge techniques: using the pipe operator (a1 | a2), and using concatenate-and-uniq ((a1 + a2).uniq). Two additional benchmarks give the time of concatenate and uniq individually.

    require 'benchmark'
    
    a1 = []; a2 = []
    [a1, a2].each do |a|
      1000000.times { a << rand(999999) }
    end
    
    puts "Merge with pipe:"
    puts Benchmark.measure { a1 | a2 }
    
    puts "Merge with concat and uniq:"
    puts Benchmark.measure { (a1 + a2).uniq }
    
    puts "Concat only:"
    puts Benchmark.measure { a1 + a2 }
    
    puts "Uniq only:"
    b = a1 + a2
    puts Benchmark.measure { b.uniq }
    

    On my machine (Ubuntu Karmic, Ruby 1.8.7), I get output like this:

    Merge with pipe:
      1.000000   0.030000   1.030000 (  1.020562)
    Merge with concat and uniq:
      1.070000   0.000000   1.070000 (  1.071448)
    Concat only:
      0.010000   0.000000   0.010000 (  0.005888)
    Uniq only:
      0.980000   0.000000   0.980000 (  0.981700)
    

    Which shows that these two techniques are very similar in speed, and that uniq is the larger component of the operation. This makes sense intuitively, being O(n) (at best), whereas simple concatenation is O(1).

    So, if you really want to speed this up, you need to look at how the <=> operator is implemented for the objects in your arrays. I believe that most of the time is being spent comparing objects to ensure inequality between any pair in the final array.

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

Sidebar

Ask A Question

Stats

  • Questions 296k
  • Answers 296k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 100% of exact trunk code? Close (disregard) your current branch… May 13, 2026 at 7:10 pm
  • Editorial Team
    Editorial Team added an answer NSLog(@"%@", [NSDate date]); The %@ specifier can accept any ObjC… May 13, 2026 at 7:10 pm
  • Editorial Team
    Editorial Team added an answer For mySQL, you want ENUM. An ENUM is a string… May 13, 2026 at 7:10 pm

Related Questions

I'm writing a merge sort function, and right now I am just using a
I have a function which takes two arrays containing the tokens/words of two texts
At work I've been dealing with some complex forms (publish pages of Symphony )
Ok, I have two tables that I am joining and doing a select on
I have a two dimensional array that I need to load data into. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.