Objective-c – Remove duplicate from NSMutableArray

I have a problem removing duplicate objects from the moving array.
I have tried these:

noDuplicates = _personalHistory.personalHistory;

for (int i=[noDuplicates count]-1; i>0; i--) {
if ([noDuplicates indexOfObject: [noDuplicates objectAtIndex: i] ] [noDuplicates removeObjectAtIndex: i];
}


for (PersonalHistory_artikels *e in _personalHistory.personalHistory) {
if (! [noDuplicates containsObject:e]) {
NSLog(@"Dubplicates");
[noDuplicates addObject:e];
}
}

< br />for (i=0; i<_personalHistory.personalHistory.count; i++) {
PersonalHistory_artikels *test = [_personalHistory.personalHistory objectAtIndex:i];
for (j=0; j<_personalHistory .personalHistory.count; j++) {
PersonalHistory_artikels *test2 = [_personalHistory.personalHistory objectAtIndex:j];
if (! [test.nieuwsTITLE_personal isEqual:test2.nieuwsTITLE_personal]) {
NSLog(@"Add test = %@", test.nieuwsTITLE_personal);
[noDuplicates addObject:test];
}
}
}

But None of the above gave me the correct array. The last one is the best, but it still shows duplicate values. Can someone help me solve this problem?
Thank you very much.

Just convert the array to NSSet and then return. Collections cannot be designed to repeat.

Edit:

Please note that collections do not have a sort order. Therefore, you can be cheaper and abandon the order, or perform slightly more expensive operations but keep Order.

NSArray *hasDuplicates = /* (...) */;
NSArray *noDuplicates = [[NSSet setWithArray: hasDuplicates] allObjects];

I have a problem removing duplicate objects from the moving array.
I have tried these:

noDuplicates = _personalHistory.personalHistory;

for (int i=[noDuplicates count]-1; i>0; i--) {
if ([noDuplicates indexOfObject: [noDuplicates objectAtIndex: i]] [noDuplicates removeObjectAtIndex: i];
}


for (PersonalHistory_artikels *e in _personalHistory.personalHistory) {
if (![noDuplicates containsObject:e]) {
NSLog(@"Dubplicates");
[noDuplicates addObject:e];
}
}


for (i=0; i<_personalHistory.personalHistory.count; i++) {
PersonalHistory_artikels *test = [_personalHisto ry.personalHistory objectAtIndex:i];
for (j=0; j<_personalHistory.personalHistory.count; j++) {
PersonalHistory_artikels *test2 = [_personalHistory.personalHistory objectAtIndex:j];
if (! [test.nieuwsTITLE_personal isEqual:test2.nieuwsTITLE_personal]) {
NSLog(@"Add test = %@", test.nieuwsTITLE_personal);
[noDuplicates addObject:test];
}
}
}

But none of the above gives me the correct array. The last one is the best, but it still shows duplicate values. Can someone help me solve this problem ?
Thank you very much.

Just convert the array to NSSet and then back. A set cannot be designed to be repeated.

Edit:

Please note that the collection does not have a sort order. Therefore, you can be cheaper and abandon the order, or perform a slightly more expensive operation but keep the order.

NSArray *hasDuplicates = /* (...) */;
NSArray *noDuplicates = [[NSSet setWithArray: hasDuplicates] allObjects];

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2191 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.