0

I have two arrays that I want to loop through. Array A and Array B. I want to add the elements of both array into a new Array that contains the collection of all elements in that array without any duplication. This will be written in python.

For Example
A = [1,2,3,4]
B = [1,5,6,7]
Collection = [1,2,3,4,5,6,7]

I was wondering if there's a faster and more efficient to do it without looping through each indexes and comparing it and then storing it. Because that's what I'm planning to do but I think it will take really long time given that I have about a couple thousand elements in each array.

Vathana Him
  • 31
  • 1
  • 4

1 Answers1

2

There is, using set:

A = [1,2,3,4]
B = [1,5,6,7]
C = set(A + B)

If you want C to be a list instead, simply convert it back afterwards:

C = list(set(A + B))
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80