Array Merge (Union)

Array Merge (Union)

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.

How to merge 2 Arrays and create a new array with unique value like Union?

You can try contating them and then filtering out alements that are already in the array

C = A.concat(B).filter((el, i, arr) => arr.findIndex(cEl => cEl.id === el.id) === i);

that will throw away the duplicates

Getting a union of two arrays in JavaScript

If you don't need to keep the order, and consider 45 and "45" to be the same:





function union_arrays (x, y) {

var obj = {};

for (var i = x.length-1; i >= 0; -- i)

obj[x[i]] = x[i];

for (var i = y.length-1; i >= 0; -- i)

obj[y[i]] = y[i];

var res = []

for (var k in obj) {

if (obj.hasOwnProperty(k)) // <-- optional

res.push(obj[k]);

}

return res;

}


console.log(union_arrays([34,35,45,48,49], [44,55]));

How to union/merge arrays of arrays in MongoDB using aggregations?

Here it is:

mongos> db.a.find()
{ "_id" : ObjectId("5ff7a8c0bfa9f0b53f4b395d"), "reqs" : [ [ "ab", "ac", "ad" ], [ "ad", "dc", "ab" ], [ "ac", "ad", "dc" ] ] }
mongos> db.a.aggregate([{ $unwind:"$reqs"} , {$unwind:"$reqs" } , {$group:{_id:null , reqs:{$addToSet:"$reqs"}}} ])
{ "_id" : null, "reqs" : [ "ac", "ad", "dc", "ab" ] }
mongos>

Union two arrays in PHP with the same structure

Use array_merge. By the way, indexes 0 and 1 are redundant at $a and $b in this example.

<?php

$a = [
0 => [
'type' => "t1",
'name' => "John",
],
1 => [
'type' => "t1",
'name' => "Jane",
]
];

$b = [
0 => [
'surname' => "Doe",
],
1 => [
'surname' => "Black",
]
];

$c = [];

// Assuming that count of $a and $b are equal.

for ($i = 0; $i < count($a); ++$i) {
$c[$i] = array_merge($a[$i], $b[$i]);
}

print_r($c);


Related Topics



Leave a reply



Submit