how to remove duplicate value in NSMutableArray

前端 未结 6 1334
离开以前
离开以前 2021-01-24 22:32

i\'m scanning wifi info using NSMutableArray, but there are few duplicate values appear, so i try to use following code but still getting the duplicate values,

         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 23:07

    I think its better to do this:

     NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc]init];
    
     for(int j = 0; j < [myArray count]; j++) {
    
     for( k = j+1;k < [myArray count];k++) {
    
        NSString *str1 = [myArray objectAtIndex:j];
        NSString *str2 = [myArray objectAtIndex:k];
        if([str1 isEqualToString:str2])
            [indexes addIndex:k];
       }
    }
    
    [myArray removeObjectsAtIndexes:indexes];
    

    You can run into problems if you manipulate the array while looping in my experience.

提交回复
热议问题