0

I have two arrays for which I am comparing [Int]

let filter = strongAgainstArray.filter{weakAgainstArray.contains($0)}

This returns an array of common values in the 2 arrays. I then want to go through and remove those values from each array, which I'm doing like so

for item in filter {
    for var i = 0; i < strongAgainstArray.count; i += 1 {
        if item == strongAgainstArray[i] {
            strongAgainstArray.removeAtIndex(i)
            print("Removing a Strong against index \(item)")
        }
    }
    for var i = 0; i < weakAgainstArray.count; i += 1 {
        if item == weakAgainstArray[i] {
            weakAgainstArray.removeAtIndex(i)
            print("Removing a Weak against index \(item)")
        }
    }
}

This works fine, but let's say one of my arrays contains two entries for 12 as an example. How do I only remove one of them? As it stands, all entries of 12 are being removed entirely.

EDIT

I'm now comparing my two arrays using

let commonValues = Array(Set(strongAgainstArray).intersect(weakAgainstArray))

and then those commonValues from each array with

cleanStrongAgainstArray =  Array(Set(strongAgainstArray).subtract(Set(commonValues)).sort())

cleanWeakAgainstArray = Array(Set(weakAgainstArray).subtract(Set(commonValues)).sort())

This is a much better overall solution, but I'm still eventually running into the same issue, albeit slightly different than before.

In the playground, for example...

let array = [7,7,9]
let test = Array(Set(array))

test comes back containing [7, 9], and I need to keep that extra 7. How do I do that?

Coltrane
  • 165
  • 1
  • 2
  • 12
  • If the values in your arrays are unique within themselves, you should consider using Sets as they have built in methods for these kinds of operations. As shown here: http://stackoverflow.com/questions/24589181/set-operations-union-intersection-on-swift-array – Ike10 Jun 23 '16 at 03:09
  • Do you need to keep the order of the array? – Paulw11 Jun 23 '16 at 03:09
  • Also one thing to note, structuring your loop like this can cause problems since you are changing the length of the array while looping through it. – john_ryan Jun 23 '16 at 03:26
  • I've switched to using Sets, and no order is not important. I've greatly simplified the code, but my problem still stands. Will update original post shortly – Coltrane Jun 23 '16 at 16:02
  • @john_ryan Thanks for pointing that out. You're right. I've corrected the issue – Coltrane Jun 23 '16 at 16:02

2 Answers2

5

If the order of the arrays aren't important then you can easily achieve the whole solution using Sets:

let dirtyArray = [1,4,6,1,56,4,4,66,23,3,3,12]
let dirtyArray1 = [3,1,6,99,54]

let cleanArray = Array(Set(dirtyArray).union(Set(dirtyArray1)))

print (cleanArray)

[12, 54, 23, 4, 6, 66, 99, 56, 1, 3]

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • This is a much cleaner solution than what I was using, but I'm still having issues. It looks like any time I'm converting an `Array` to `Set` It's combining common values. `[7, 7, 9]`, for example, becomes `[7, 9]` after converting – Coltrane Jun 23 '16 at 16:05
2

If order is important, use NSOrderedSet:

let strongAgainstArray = [1, 2, 3, 4]
let weakAgainstArray = [3, 4, 5, 6]

let result = NSOrderedSet(array: (strongAgainstArray + weakAgainstArray)).array
Code Different
  • 90,614
  • 16
  • 144
  • 163