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?

5
  • 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: stackoverflow.com/questions/24589181/… Commented Jun 23, 2016 at 3:09
  • Do you need to keep the order of the array? Commented Jun 23, 2016 at 3: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. Commented Jun 23, 2016 at 3: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 Commented Jun 23, 2016 at 16:02
  • @john_ryan Thanks for pointing that out. You're right. I've corrected the issue Commented Jun 23, 2016 at 16:02

2 Answers 2

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]

Sign up to request clarification or add additional context in comments.

1 Comment

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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.