Array Merge without Duplicates

15

3

I recently saw this Javascript code on StackOverflow for merging two arrays, and removing duplicates:

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }
    return a;
};

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
var array3 = array1.concat(array2).unique(); 

While this code works, it is horribly inefficient (O(n^2)). Your challenge is to make an algorithm with less complexity.

The winning criteria is the solution with the least complexity, but ties will be broken by shortest length in characters.

Requirements:

Package all your code together in a function that meets the following requirements for "correctness:"

  • Input: Two arrays
  • Output: One array
  • Merges elements of both arrays together- Any element in either input array must be in the outputted array.
  • The outputted array should have no duplicates.
  • Order doesn't matter (unlike the original)
  • Any language counts
  • Don't use the standard library's array functions for detecting uniqueness or merging sets/arrays (although other things from the standard library is okay). Let me make the distinction that array concatenation is fine, but functions that already do all of the above are not.

hkk

Posted 2014-01-02T00:53:37.540

Reputation: 261

How are we supposed to create or append to an array without using array functions? – Emil Vikström – 2014-01-02T01:11:16.540

@EmilVikström See my edit. I meant that you can't use array uniqueness functions. Sorry for being unclear. – hkk – 2014-01-02T01:12:56.317

If one of the arrays has duplicates in it, do we remove them as well? For example, should merging [1, 2, 2, 3] and [2, 3, 4] return [1, 2, 2, 3, 4] or [1, 2, 3, 4]? – O-I – 2014-01-02T01:18:02.953

@O-I The second output is the correct one. The outputted array should have no duplicates. – hkk – 2014-01-02T01:18:49.323

So, I'm guessing simply doing arr1 | arr2 in Ruby is against the rules? – O-I – 2014-01-02T01:22:27.347

1@O-I Yes, that would make it too easy. – hkk – 2014-01-02T01:24:12.313

1May I ask: Arrays of what? Can we assume simply integers or strings, or do we also have to allow more complex things like multilevel objects? – jawns317 – 2014-01-02T01:30:34.117

@jawns317 Assume that the components of the arrays are primitives- integers, strings, floats, or bools. – hkk – 2014-01-02T01:32:29.377

Does using a function like Mathematica's DeleteDuplicates count as a uniqeness function? – Yves Klett – 2014-01-02T08:11:11.567

@YvesKlett Yes. That kind of defeats the whole purpose of finding a more efficient algorithm. – hkk – 2014-01-02T15:46:55.450

It need to realize a merge sort with Worst-case performance ~ O(n log n). Boring.

– mazzy – 2018-09-26T09:22:16.687

Answers

8

Perl

27 Characters

Simple Perl Hack

my @vals = ();
push @vals, @arr1, @arr2;
my %out;
map { $out{$_}++ } @vals;
my @unique = keys %out;

I'm sure someone could one-liner this.. and thus (Thanks Dom Hastings)

sub x{$_{$_}++for@_;keys%_}

Zach Leighton

Posted 2014-01-02T00:53:37.540

Reputation: 194

A bit late to the party: sub x{keys{map{$_,1}@_}} for 24. The original could also be improved by one by replacing $_{$_}++ with \$_{$_}. – primo – 2016-04-20T11:06:20.067

1"Don't use the standard library's array functions for detecting uniqueness (although other things form the standard library is okay)" – John Dvorak – 2014-01-02T07:17:11.017

1How I am violating that rule? I'm not using unique functions? – Zach Leighton – 2014-01-02T11:51:37.793

How does it work, then? Sorry, I can't read perl. If it reads the keys of a hash map - does that count as OK with that rule? I won't vote until convinced that it is. – John Dvorak – 2014-01-02T11:54:09.263

1It combines the arrays, loops over both and adds to a hash incrementing the value who's key is the current value in the array loop. Then it takes the keys of that hash, I have used this in some of my work..

So [1,1,2,3,4,4] becomes { 1 => 2, 2 => 1, 3 => 1, 4 => 2 } – Zach Leighton – 2014-01-02T11:59:56.790

@ZachLeighton you can shorten the code to 27 chars with sub x{$_{$_}++for@_;keys%_} (in case it comes down to a tie!) and use as: z((1,2,3,4),(2,3,4,5,6)) – Dom Hastings – 2014-01-02T12:13:44.063

I saw your answer above.. very nicely done. – Zach Leighton – 2014-01-02T12:21:06.723

I've removed my answer, I didn't realise you'd already submitted a Perl solution. Please feel free to utilise this golfed version. – Dom Hastings – 2014-01-02T12:22:29.380

Sure I referenced you in the edit, thanks! – Zach Leighton – 2014-01-02T12:23:24.170

10

JavaScript O(N) 131 124 116 92 (86?)

Golfed version:

function m(i,x){h={};n=[];for(a=2;a--;i=x)i.map(function(b){h[b]=h[b]||n.push(b)});return n}

Human readable golfed version:

function m(i,x) {
   h = {}
   n = []
   for (a = 2; a--; i=x)
      i.map(function(b){
        h[b] = h[b] || n.push(b)
      })
   return n
}

I could use concat like so and do it in 86 characters:

function m(i,x){h={};n=[];i.concat(x).map(function(b){h[b]=h[b]||n.push(b)});return n}

But I am not sure if it is still O(N) based on this JsPerf: http://jsperf.com/unique-array-merging-concat-vs-looping as the concat version is marginally faster with smaller arrays but slower with larger arrays (Chrome 31 OSX).

In practice do this (golf is full of bad practices):

function merge(a1, a2) {
   var hash = {};
   var arr = [];
   for (var i = 0; i < a1.length; i++) {
      if (hash[a1[i]] !== true) {
        hash[a1[i]] = true;
        arr[arr.length] = a1[i];
      }
   }
   for (var i = 0; i < a2.length; i++) {
      if (hash[a2[i]] !== true) {
        hash[a2[i]] = true;
        arr[arr.length] = a2[i];
      }
   }
   return arr;
}
console.log(merge([1,2,3,4,5],[1,2,3,4,5,6]));

I'm not great at computing complexity but I believe this is O(N). Would love if someone could clarify.

Edit: Here is a version that takes any number of arrays and merges them.

function merge() {
   var args = arguments;
   var hash = {};
   var arr = [];
   for (var i = 0; i < args.length; i++) {
      for (var j = 0; j < args[i].length; j++) {
        if (hash[args[i][j]] !== true) {
          arr[arr.length] = args[i][j];
          hash[args[i][j]] = true;
        }
      }
    }
   return arr;
}
console.log(merge([1,2,3,4,5],[1,2,3,4,5,6],[1,2,3,4,5,6,7],[1,2,3,4,5,6,7,8]));

George Reith

Posted 2014-01-02T00:53:37.540

Reputation: 2 424

This is almost exactly what I was gonna post in a couple of seconds :-( Yes, it is amortized linear time if hash tables are implemented with amortized constant time for insertion and searching (which is common in many languages, don't know specifically about JS). – Emil Vikström – 2014-01-02T01:14:08.063

@EmilVikström Thanks for that I believe JavaScript does but don't have proof on it. Apologies for having fast fingers, slowed yourself down with comments :P – George Reith – 2014-01-02T01:17:42.217

This is a great approach. However, could you also provide a "code-golf" style solution in addition to your nicely formatted version? Seeing that multiple people have thought of this as the right approach, there's probably going to be a tie at O(N). – hkk – 2014-01-02T01:21:50.193

@cloudcoder2000 Ok, I wanted to print a full version as the code-golf version is likely to be less efficient in practice. – George Reith – 2014-01-02T01:24:49.310

@GeorgeReith I'm not really sure what you meant by that... but what I meant by "code-golf" style is that you minify, simplify, and shorten your current O(N) solution. – hkk – 2014-01-02T01:26:29.337

@GeorgeReith You're second solution appears to be O(N^2). – hkk – 2014-01-02T01:26:57.760

@cloudcoder2000 It is no different from the first. The only complexity added is looping the argument list. Each array only gets looped once still. It just loops the arg list instead of explicitly stating each one. I get what you mean by code-golf I will post one, what I meant is I wanted a normal version as code-golf enforces bad practices which equal poorer performance at execution time. – George Reith – 2014-01-02T01:37:17.813

@GeorgeReith Thanks for clarifying, and I look forward to seeing what you post. Also, when you have nested for loops, you're pretty much guaranteed to run in polynomial time. – hkk – 2014-01-02T01:40:02.077

@cloudcoder2000 Added golfed version. I'd love to get some clarification on that but I can't see how there is any difference between the first and second version. With two arrays it still executes a loop on the array once each time. The same code gets executed, just in the first I wrote two seperate loops here I have an outer loop that just runs the inner loop twice. Except with the bonus of supplying an unlimited number of arrays to merge. – George Reith – 2014-01-02T01:50:24.573

@GeorgeReith With only two arrays as input, that is what happens. However, when calculating time complexity, you always look for the worst case scenario. Nested loops are multiplied, so the worst case is O(n^2). I'm not knowledgeable enough about it to explain to you (I don't want to say anything incorrect and give you the wrong idea) but you can look at this link for more info.

– hkk – 2014-01-02T01:56:48.830

@cloudcoder2000 According to the requirements two arrays is the worst possible scenario, this is a controlled input scenario. Seems odd to me. Although I could remove args.length and replace it with 2 if you like... would actually save characters. – George Reith – 2014-01-02T02:00:57.600

@GeorgeReith It is true that for your first function, the worst case is two inputs. However, when you say that the function "takes any number of arrays" then you imply that the input for the function can be multiple arrays- polynomial time. If you mean that your function is only meant for two arrays- linear time- as input, then please describe it accordingly. – hkk – 2014-01-02T02:05:29.177

@cloudcoder2000 Okay... I actually re-implemented my golfed version which is limited to two arrays (saved me some chars). Thanks for the info, I'm sure you're correct it just seems odd to me. – George Reith – 2014-01-02T02:09:27.387

@cloudcoder2000 @GeorgeReith Looks like it would be O(N) to me even with variable inputs, but you need to be careful with your definition of N. It would be the number of elements contained in all input arrays, not how many input arrays there are. – meiamsome – 2014-01-02T03:12:56.947

Why do you have 1 + -- i instead of just i--? – meiamsome – 2014-01-02T03:26:33.413

@meiamsome there are two loops, and each one's worst case is independent from the other. So really, shouldn't it be O(N*M)? I'm not certain of this, so please clarify. Either way, it doesn't really matter, because @GeorgeReith provided another solution that is definitively O(N). – hkk – 2014-01-02T04:06:40.440

1@cloudcoder2000 They are not fully independent so the worst case is not O(A*B) (Not using N because it's confusing). It would be that if every input array (every A) had the same amount of elements (B) as it is actually O(SUM(B) FOR ALL A), which can be rewritten as O(N) when defining N as the count of elements of all array inputs. – meiamsome – 2014-01-02T04:17:14.490

@cloudcoder2000 Consider m([1,1],[1,1,1,1]) and m([1,1],[1,1],[1,1]). Both would run in almost the same time. This can be extended to any amount of arguments/elements and you can see time is independent of the amount of arguments but dependant on how many elements in all the arrays. They would run at slightly different times due to how many loops are ran and thus how many times variables are altered, but such differences are negligible when using big-O notation assuming B >> A. – meiamsome – 2014-01-02T04:19:19.100

@meiamsome That's great to know - that's what my gut tells me, you're definitely better at expressing it than I. 1+--i because I moved the --i into my condition and it would ignore 0 otherwise because the condition gets called before the first iteration - Just squeezing out characters. – George Reith – 2014-01-02T07:07:54.033

@GeorgeReith i-- would perform the loop with i being 1 and then 0 as well, no? – meiamsome – 2014-01-02T07:20:10.153

@meiamsome You're right... that's two less characters. I had a reason I couldn't use i-- in one of my earlier versions... I forgot that had changed. Thank you sir. – George Reith – 2014-01-02T07:41:21.283

You should put the golfed version on top since that is the one that's actually 92 characters. Currently, the first code listed contains something like 250 characters. – Kyle Kanos – 2014-01-02T15:54:25.390

@KyleKanos Done :) – George Reith – 2014-01-02T16:18:47.203

function m(i,x){h={};i.concat(x).map(function(b){h[b]=1});return Object.keys(h)} – l4m2 – 2018-10-26T16:03:48.960

4

Python 2.7, 38 chars

F=lambda x,y:{c:1 for c in x+y}.keys()

Should be O(N) assuming a good hash function.

Wasi's 8 character set implementation is better, if you don't think it violates the rules.

Keith Randall

Posted 2014-01-02T00:53:37.540

Reputation: 19 865

Nice! Comprehensions in Python can be so elegant and powerful. – O-I – 2014-01-02T17:23:21.003

3

One way in Ruby

To keep within the rules outlined above, I would use a similar strategy as the JavaScript solution and use a hash as an intermediary.

merged_arr = {}.tap { |hash| (arr1 + arr2).each { |el| hash[el] ||= el } }.keys

Essentially, these are the steps I'm going through in the line above.

  1. Define a variable merged_arr that will contain the result
  2. Initialize an empty, unnamed hash as an intermediary to put unique elements in
  3. Use Object#tap to populate the hash (referenced as hash in the tap block) and return it for subsequent method chaining
  4. Concatenate arr1 and arr2 into a single, unprocessed array
  5. For each element el in the concatenated array, put the value el in hash[el] if no value of hash[el] currently exists. The memoization here (hash[el] ||= el) is what ensures the uniqueness of elements.
  6. Fetch the keys (or values, as they are the same) for the now populated hash

This should run in O(n) time. Please let me know if I've made any inaccurate statements or if I can improve the above answer either for efficiency or readability.

Possible improvements

Using memoization is probably unnecessary given that the keys to the hash are going to be unique and the values are irrelevant, so this is sufficient:

merged_arr = {}.tap { |hash| (arr1 + arr2).each { |el| hash[el] = 1 } }.keys

I really love Object#tap, but we can accomplish the same result using Enumerable#reduce:

merged_arr = (arr1 + arr2).reduce({}) { |arr, val| arr[val] = 1; arr }.keys

You could even use Enumberable#map:

merged_arr = Hash[(arr1 + arr2).map { |val| [val, 1] }].keys

How I would do it in practice

Having said all that, if I were asked to merge two arrays arr1 and arr2 such that the result merged_arr has unique elements and could use any Ruby method at my disposal, I would simply use the set union operator which is intended for solving this exact problem:

merged_arr = arr1 | arr2

A quick peek at the source of Array#|, though, seems to confirm that using a hash as an intermediary seems to be the acceptable solution to performing a unique merge between 2 arrays.

O-I

Posted 2014-01-02T00:53:37.540

Reputation: 759

"Don't use the standard library's array functions for detecting uniqueness (although other things form the standard library is okay)" – John Dvorak – 2014-01-02T11:55:51.070

How am I violating that rule in the second example? Memoization is being performed on a hash. Is that not allowed either? – O-I – 2014-01-02T13:54:57.863

3

PHP, 69/42 68/41 chars

Including the function declaration is 68 characters:

function m($a,$b){return array_keys(array_flip($a)+array_flip($b));}

Not including the function declaration is 41 characters:

array_keys(array_flip($a)+array_flip($b))

zamnuts

Posted 2014-01-02T00:53:37.540

Reputation: 263

2

Array.prototype.unique = function()
{
  var o = {},i = this.length
  while(i--)o[this[i]]=true
  return Object.keys(o)
}

A function that would take n arrays could be the following:

function m()
{
  var o={},a=arguments,c=a.length,i;
  while(c--){i=a[c].length;while(i--)o[a[c][i]] = true} 
  return Object.keys(o);
}

Golfed, I think this should work ( 117 chars )

function m(){var o={},a=arguments,c=a.length,i;while(c--){i=a[c].length;while(i--)o[a[c][i]]=1}return Object.keys(o)}

Update If you want to keep the original type, you could

function m()
{
  var o={},a=arguments,c=a.length,f=[],g=[];
  while(c--)g.concat(a[c])
  c = g.length      
  while(c--){if(!o[g[c]]){o[g[c]]=1;f.push(g[c])}}
  return f
}

or golfed 149:

function m(){var o={},a=arguments,c=a.length,f=[],g=[];while(c--)g.concat(a[c]);c= g.length;while(c--){if(!o[g[c]]){o[g[c]]=1;f.push(g[c])}}return f}

This still can cast some doubts, if you want to distinguish 123 and '123', then this would not work..

Konijn

Posted 2014-01-02T00:53:37.540

Reputation: 505

Thanks for the answer. It is impressively short, however this only does half the problem. You also need to include in the solution the actual merging part (even if its the same as in the original example) and put it all together in one function. Also, could you provide "golfed" version in addition to this (as it is O(N))? – hkk – 2014-01-02T02:16:30.047

This casts all members into strings. e.g. m([1,2,3,4,5],[2,3,4,5,6],[2,3,4,5,6,7]) becomes ["1", "2", "3", "4", "5", "6", "7"] – George Reith – 2014-01-02T02:22:06.457

2

Mathematica 10 Chars

Union[a,b]

Example:

a={1,2,3,4,5};
b={1,2,3,4,5,6};
Union[a,b]

{1, 2, 3, 4, 5, 6}

Mathematica2 43 Chars

Sort@Join[a, b] //. {a___, b_, b_, c___} :> {a, b, c}

Murta

Posted 2014-01-02T00:53:37.540

Reputation: 339

8I think this would go in the category of using standard library array methods. – hkk – 2014-01-02T04:02:16.423

Hi @cloudcoder2000. No need to call some specific library to use Union in Mathematica. – Murta – 2014-01-02T10:44:12.857

5In my opinion, using a builtin function to do exactly what the question is asking to do is cheating. – Konrad Borowski – 2014-01-02T14:37:24.087

ok ok .. the second code do not use Union. – Murta – 2014-01-02T16:57:54.730

1I guess Tally[Join[a, b]][[;; , 1]] would also be cheating ;-) BTW you could save chars by using single-letter variables. – Yves Klett – 2014-01-02T19:22:54.287

@YvesKlett! Done!.. – Murta – 2014-01-02T19:59:36.813

2

python,46

def A(a,b):print[i for i in b if i not in a]+a

Or, using set operation simply

python, 8

set(a+b)

Wasi

Posted 2014-01-02T00:53:37.540

Reputation: 1 682

1Sorry it wasn't clear, using set operations is also cheating. – hkk – 2014-01-02T19:09:41.917

Your 1st code will have duplicates if there are duplicates in a or if there are duplicates in b and that element is not in a. – Vedant Kandoi – 2018-10-29T12:14:36.300

2

Fortran: 282 252 233 213

Golfed version:

function f(a,b,m,n) result(d);integer::m,n,a(m),b(n),c(m+n);integer,allocatable::d(:);j=m+1;c(1:m)=a(1:m);do i=1,n;if(.not.any(b(i)==c(1:m)))then;c(j)=b(i);j=j+1;endif;enddo;allocate(d(j-1));d=c(1:j-1);endfunction

Which not only looks infinitely better but will actually compile (too long a line in its golfed form) with the human-readable form:

function f(a,b,m,n) result(d)
  integer::m,n,a(m),b(n),c(m+n)
  integer,allocatable::d(:)
  j=m+1;c(1:m)=a(1:m)
  do i=1,n
     if(.not.any(b(i)==c(1:m)))then
        c(j)=b(i);j=j+1
     endif
  enddo
  allocate(d(j-1))
  d=c(1:j-1)
end function

This should be O(n) as I copy a into c and then check each b against all of c. The last step is to eliminate the garbage that c will contain since it is uninitialized.

Kyle Kanos

Posted 2014-01-02T00:53:37.540

Reputation: 4 270

2

Perl

23 bytes, if we only count the code block inside subroutine. Could be 21, if overwriting global values is allowed (it would remove my from the code). It returns elements in random order, because order doesn't matter. As for complexity, on average it's O(N) (depends on number of hash collisions, but they are rather rare - in worst case it can be O(N2) (but this shouldn't happen, because Perl can detect pathological hashes, and changes the hash function seed when it detects such behavior)).

use 5.010;
sub unique{
    my%a=map{$_,1}@_;keys%a
}
my @a1 = (1, 2, 3, 4);
my @a2 = (3, 4, 5, 6);
say join " ", unique @a1, @a2;

Output (also showing randomness):

/tmp $ perl unique.pl 
2 3 4 6 1 5
/tmp $ perl unique.pl 
5 4 6 2 1 3

Konrad Borowski

Posted 2014-01-02T00:53:37.540

Reputation: 11 185

1

Javascript 86

Golfed version:

function m(a,b){var h={};return a.concat(b).filter(function(v){return h[v]?0:h[v]=1})}

Readable version:

function merge(a, b) {
  var hash = {};
  return a.concat(b).filter(function (val) {
    return hash[val] ? 0 : hash[val] = 1;
  });
}

Bertrand

Posted 2014-01-02T00:53:37.540

Reputation: 111

1This ignores falsey values... m([1,0,0,0,0],[0,1,0]) returns [1]. – George Reith – 2014-01-03T12:30:29.453

1Change h[v]=v to h[v]=1. – George Reith – 2014-01-03T12:36:27.133

Well spotted @GeorgeReith! We went from 86 to 84 :) – Bertrand – 2014-01-03T12:42:39.723

It's still 86, I think you got confused because you removed 2 characters from the readable version not the golfed one. – George Reith – 2014-01-03T15:37:24.663

1

JavaScript 60

I'm using ES6 generator.
The following is testable using Google's Traceur REPL.

m=(i,j)=>{h={};return[for(x of i.concat(j))if(!h[x])h[x]=x]}

Florent

Posted 2014-01-02T00:53:37.540

Reputation: 2 557

0

If you're looking for a JavaScript-based implementation that relies on the underlying Objects behind the framework to be efficient, I would've just used Set. Usually in an implementation, the Set object inherently handles unique objects during insertion with some sort of binary search indexing. I know in Java it's a log(n) search, using binary search based on the fact that no set can contain a single object more than once.


While I have no idea if this is also true for Javascript, something as simple as the following snippet may suffice for an n*log(n) implementation:

JavaScript, 61 bytes

var s = new Set(a);      // Complexity O(a.length)
b.forEach(function(e) {  // Complexity O(b.length) * O(s.add())
  s.add(e);
}); 

Try it online!


If the above snippet uses a = [1,2,3] and b = [1,2,3,4,5,6] then s=[1,2,3,4,5,6].

If you know the complexity of the Set.add(Object) function in JavaScript let me know, the complexity of this is n + n * f(O) where f(O) is the complexity of s.add(O).

Magic Octopus Urn

Posted 2014-01-02T00:53:37.540

Reputation: 19 422

0

APL (Dyalog Unicode), O(N), 28 bytes

Anonymous tacit infix function.

(⊢(/⍨)⍳∘≢=⍳⍨),

Try it online!

, concatenate the arguments; O(N)

() apply the following anonymous tacit function on that; O(1)

   ⍳⍨ indices selfie (indices of first occurrence of each element in the entire array); O(N)

  = compare element by element to; O(N):

   ⍳∘≢ indices of the length of the array; O(N)

(/⍨) use that to filter; O(N):

   the unmodified argument; O(1)

O(N + 1 + N + N + N + N + 1) = O(N)

Adám

Posted 2014-01-02T00:53:37.540

Reputation: 37 779

-2

JavaScript, 131 characters

var array1 = ["Vijendra","Singh"];   
var array2 = ["Singh", "Shakya"];     
result = Array.from(new Set([...array1, ...array2]))

deepak_pal

Posted 2014-01-02T00:53:37.540

Reputation: 1

4Welcome to PPCG! Please tell us which language this is and format it as code for better readability. (This works by indenting the code lines with four spaces). Also an explanation of your approach would be appreciated. – Laikoni – 2018-10-26T13:01:59.090

it is just a javascript code. – deepak_pal – 2018-10-29T07:47:18.297

@techdeepak You could add such vital information to your post, properly format it, add syntax highlighting and write a bit more about your algorithm's complexity, as this is [tag:fastest-algorithm]. As it stands, this post is of fairly low quality. – Jonathan Frech – 2018-10-29T10:00:39.357

-2

PHP around 28 characters [ leaving out the example array variables and result variable ].

$array1 = array(1, 2, 3); $array2 = array(3, 4, 5);

$result = array_merge($array1, $array2);

Endri

Posted 2014-01-02T00:53:37.540

Reputation: 1

From the question: Don't use the standard library's array functions for detecting uniqueness or merging sets/arrays. Additionally this doesn't actually remove duplicates from the array – Jo King – 2019-02-05T09:05:11.203

I think you've overlooked this important line from the question: "Don't use the standard library's array functions for detecting uniqueness or merging sets/arrays" – Peter Taylor – 2019-02-05T09:05:24.247

Yes. That's correct. Thank you guys for pointed that out. Critiques humbly accepted. – Endri – 2019-02-06T08:25:11.540

@jo king. You are absolutely right about "Don't use standard library's...". The rest is wrong. It does remove the duplicates. http://php.net/manual/en/function.array-merge.php. I recommend you to fully read the PHP's documentation. I'm 100% sure it does the job. Your just need to be careful which one of the arrays you consider as duplicates. Cheers.

– Endri – 2019-02-06T08:30:29.443

1

I literally ran the code in your submission with no changes and the output has duplicates. Looks like you should read the documentation, namely If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended

– Jo King – 2019-02-06T08:36:25.010