0

I have 2 NSMutableArrays. They are:

allStudentsMutableArr
selectedStudentsMutableArr

allStudentsMutableArr contains all the Student objects and it has around 100 objects saved in it. Whereas, selectedStudentsMutableArr only contains 10 Student records saved in it.

Now i need to compare the 2 MutableArrays and remove all objects from allStudentsMutableArr that's present in selectedStudentsMutableArr mutable array. How can i do this ?

Illep
  • 16,375
  • 46
  • 171
  • 302
  • possible duplicate of [NSArray with NSPredicate using NOT IN](http://stackoverflow.com/questions/8580715/nsarray-with-nspredicate-using-not-in) – Paulw11 Mar 17 '15 at 11:03
  • 1
    `NSMutableSet` and `–minusSet:`... IMHO is the best way to do such thing. – holex Mar 17 '15 at 11:24

2 Answers2

2

Simple

[allStudentsMutableArr removeObjectsInArray:selectedStudentsMutableArr];
Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
0

You can also use NSPredicate as below...(Gadhiya solution also useful..)

Suppose you have student object with name as one of property then..//or any property that you want to compare and filter..

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat: @"!(name IN %@)", selectedStudentsMutableArr];
NSArray* filteredArray = [allStudebtsMutableArr filteredArrayUsingPredicate:filterPredicate];

Hope it helps you..

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59