0

I am using subtracting for getting un-common element which is working fine.

but not working if both array has different number of elements (count)

like

I have array

fileprivate var daysSelected:[Schedule.ScheduleDays] = [] {
    didSet {
        if let array =  equipment.schedules?.map({$0.day}), Set(array).subtracting(Set(daysSelected)).count > 0{
            debugPrint("After updated days ",array)
            daySelectionChanged = true
        } else {
            daySelectionChanged = false

        }
        debugPrint("After updated days ",self.daysSelected)

    }
}

enum ScheduleDays: String {
    case everyday
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday
    case sunday
}

If array has less element then daysSelected it is returns 0

So Is there any work around that get un - common element from two array which has different count without check such a array.count > daysSelected

Example Suppose daysSelected has

  - 0 : App.Schedule.ScheduleDays.tuesday
  - 1 : App.Schedule.ScheduleDays.thursday
  - 2 : App.Schedule.ScheduleDays.friday
  - 3 : App.Schedule.ScheduleDays.saturday
  - 4 : App.Schedule.ScheduleDays.sunday
  - 5 : App.Schedule.ScheduleDays.wednesday
  - 6 : App.Schedule.ScheduleDays.monday

and another set has

  - 0 : App.Schedule.ScheduleDays.friday
  - 1 : App.Schedule.ScheduleDays.saturday
  - 2 : App.Schedule.ScheduleDays.sunday
  - 3 : App.Schedule.ScheduleDays.tuesday
  - 4 : App.Schedule.ScheduleDays.wednesday

When I

Set(equipment.schedules!.map({$0.day})).subtracting(Set(daysSelected))
0 elements

and

 po Set(daysSelected).subtracting(Set(equipment.schedules!.map({$0.day})))
▿ 2 elements
  - 0 : App.Schedule.ScheduleDays.thursday
  - 1 : App.Schedule.ScheduleDays.monday
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • 1
    https://stackoverflow.com/questions/24589181/set-operations-union-intersection-on-swift-array ? What you want is not `union` nor `intersection` but `symmetricDifference`? – Larme Jun 19 '18 at 11:02
  • @Larme What is both array has different count let me update with more context – Prashant Tukadiya Jun 19 '18 at 11:03
  • @Larme symmetricDifference is what I am looking thanks for quick help :) Please add answer I will accept it – Prashant Tukadiya Jun 19 '18 at 11:06
  • I flagged the question as duplicate. If you think that yours doesn't provide more help you can delete it. If you think that users might easier find yours instead of the linked one (choice of words to describe your issue, etc.) and then could be redirected, then mark it as duplicate yourself (it's quicker than waiting for other 4 flag/votes)). – Larme Jun 19 '18 at 11:09
  • @Larme yes I have marked my answer as duplicate, But Yes you are right with given link of answer has not provided that enough information or that not well documented that user can easily catch his required function .However I have explained my problem well enough as per my knowledge (my english skills :D ) – Prashant Tukadiya Jun 19 '18 at 11:12

0 Answers0