0

I have two arrays:

var tstFrames = [TimeFrame]()
var tstFramesRefresh = [TimeFrame]()

Does anybody know how to compare them?

I found some examples adding array to Set, but Set doesn't have initializer which can accept this arraytype.. :(

The two array has almost the same instances of TimeFrame object but tstFramesRefresh has one more which is not in tstFrames array i would get that one TimeFrame object.

4
  • 1
    Are you trying to determine if these arrays contain the same items? What are you trying to do exactly? Commented May 23, 2015 at 12:33
  • sorry, i update my question.. Commented May 23, 2015 at 12:36
  • 1
    You could a Set, like you suggested, you'd just need to make your type conform to Hashable. Commented May 23, 2015 at 12:37
  • thank you, could you please send me any tutorial how to do it? Commented May 23, 2015 at 12:39

1 Answer 1

4

When you say “compare” – do you mean equal, lexicographic comparison, or diff? Sounds like you want a diff.

The set solution might look like this (assuming you want things in one not in the other, rather than the disjunction of the two):

let a = [1,2,3]
let b = [1,2]

Set(a).subtract(b)  // returns a set of {3}

Presumably your issue is that TimeFrame is not Hashable, hence it won’t work inside a set.

You could instead use contains to check if each element of a were a member of b. Unfortunately this will be quite inefficient for large sets, since contains works in linear time so complexity will be O(n^2). But it’s easy to do:

func subtract<S: SequenceType, C: CollectionType, T: Equatable
    where C.Generator.Element == T, S.Generator.Element == T>
    (source: S, toSubtract: C) -> [S.Generator.Element] {
        return filter(source) { !contains(toSubtract, $0) }
}


subtract(a, b)  // returns [3]

More efficient solutions if your object were Comparable could use sorting or a tree structure.

If your objects aren’t Equatable either, you’d need a version that took a closure to check for equivalence:

func subtract<S: SequenceType, C: CollectionType>
    (source: S, toSubtract: C,
     isEquivalent: (S.Generator.Element, C.Generator.Element) -> Bool)
    -> [S.Generator.Element] {
        let x = filter(source) { elem in
            return !contains(toSubtract) { isEquivalent(elem,$0) }
        }
        return x
}

subtract(a, b) { $0 == $1 }

If you just want to check if they are equal, then == will do it – unless the objects aren’t equatable, in which case use the equal function that takes a comparator function:

// asuming these are classes and reference equality is reasonable thing to check
equal(tstFrames, tstFramesRefresh) { $0 === $1 } 
Sign up to request clarification or add additional context in comments.

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.