A mutablearray populated with items from a table with 3 fields (class number, lessons and started). I want to keep only one of the items with the same class number, say in one case, there are 3 objects with same class number. What is the best way to remove any of the two items.
Asked
Active
Viewed 466 times
-1
-
For example, there are 3 objects as below. The second and third objects are not exactly the same. They only have the same class number. I want to remove one of them, so that I shall have only one class 2. class number (1) lessons (15) started (4th), class number (2) lessons (16) started (4th), class number (2) lessons (17) started (5th). – Gamma Ko Aug 23 '11 at 13:08
1 Answers
1
You can easily google it. Let me do it for you.
See the following example :
-
Thanks for your answer. Yes, I saw these posts. I thought it can not solve my problem. It only removes duplicate objects and not duplicate of one of the field value. – Gamma Ko Aug 23 '11 at 11:57
-
The following example only remove duplicate object? My problem is duplicate field values but the objects are not exactly the same. – Gamma Ko Aug 23 '11 at 12:02
-
NSArray *copy = [mutableArray copy]; NSInteger index = [copy count] - 1; for (id object in [copy reverseObjectEnumerator]) { if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) { [mutableArray removeObjectAtIndex:index]; } index--; } [copy release]; – Gamma Ko Aug 23 '11 at 12:03