1. 程式人生 > >去掉陣列中的重複物件

去掉陣列中的重複物件

//resultArray是需要去重複的目標陣列

        NSMutableArray *uniqueArray = reslutArray;

        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];

        NSMutableIndexSet *removeSet = [NSMutableIndexSet indexSet];

        int index = 0;

        for(YDMsgFileInfo * obj in uniqueArray)

        {

            if(dict[obj.fileId] == nil){

                dict[obj.fileId] = @YES;

            }else{

                [removeSet addIndex:index];

            }

            index++;

        }

        [uniqueArray removeObjectsAtIndexes:removeSet];

其他簡單型別的陣列去重複方法

NSArray *array = @[@"name", @"w", @"aa", @"zxp", @"aa"]; //返回的是一個新的陣列 NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.self"]; NSLog(@"%@", newArray);