-1

If I have two lists for example list1 and list2 how would I search list1 to see if there were any elements which were not in list2 then append it to list2. as an example here are both lists:

let list1 = ["James","John","fat","cart"]
var list2 = ["James","fat","bobby"]

therefore after the algorithm list2 would be:

["James","fat","bobby","John,"cart"]
  • Duplicate of https://stackoverflow.com/questions/30685163/compare-2-arrays-and-list-the-differences-swift ? – Underverse Dec 31 '18 at 11:37
  • 2
    Possible duplicate of [Compare 2 arrays and list the differences - Swift](https://stackoverflow.com/questions/30685163/compare-2-arrays-and-list-the-differences-swift) – Matt Ke Dec 31 '18 at 12:01
  • Show the code you’ve already tried. We don’t write the code for you. – Magnas Dec 31 '18 at 12:44
  • You should mark as accepted which answer worked for you.. – delavega66 Jan 02 '19 at 11:41

4 Answers4

2
for value in list1 {
  if !list2.contains(value) {
    list2.append(value)
  }
}

OR

list1.forEach({ !list2.contains($0) ? list2.append($0) : nil })

OR

list2.append(contentsOf: Set(list1).subtracting(Set(list2)))

Use a foreach loop to iterate through the items in list1. If list2 does not contain an item, then append it to list2.*

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
nimesh surani
  • 177
  • 1
  • 13
1

You can do it one line coding

let list1 = ["James","John","fat","cart"]
var list2 = ["James","fat","bobby"]

list2.append(contentsOf: list1.filter{!list2.contains($0)})
print("\list2")

//output
["James", "fat", "bobby", "John", "cart"]
delavega66
  • 855
  • 2
  • 23
  • 39
1

Most of the answers so far will have poor performance on large arrays.

The Array.contains(_:) method has O(n) performance, meaning that combining 2 arrays will have O(n•m) performance (where n is the number of items in one array and m is the number of items in the other array.) This the time performance will deteriorate exponentially as the arrays grow in size.

If you only need to deal with a handful of items, this doesn't matter. But with >= hundreds of items in the arrays the time performance will get bad fast.

Better to use Sets:

let list1 = ["James","John","fat","cart"]
var list2 = ["James","fat","bobby"]

list2 = Array(Set(list1).union(Set(list2)))

print(Set([list1, list2]))
print(list2)

Sets use hashes for contains/uniqueness testing, which runs in ≈ constant time.

Jorgandar's answer should have good time-performance as well, but mine is a little simpler and (I think) more straightforward to understand.

I just convert each array into a Set, combine them with Set.union(_:), and then convert the results back to a set.

If preserving the original order is important then you could build sets out of each array for uniqueness testing and then loop through one of the arrays using the sets to tell if each item needs to be added to the other array. That would be a little more complex but should give ≈O(n) performance and preserve array ordering.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You need to use Set like this way:

let list1 = ["James","John","fat","cart"]
let list2 = ["James","fat","bobby"]

let final = Array(Set([list1, list2].flatMap({$0})))

Or

let final = Array(Set(list1 + list2))
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26