如何从数组中删除重复值

时间:2022-08-26 14:54:39

Hello i have one NSMUtableArry which containing duplicates value e.g.[1,2,3,1,1,6].I want to remove duplicates value and want new array with distinct values.Please help

您好我有一个NSMUtableArry包含重复值,例如[1,2,3,1,1,6]。我想删除重复值并希望新数组具有不同的值。请帮助

6 个解决方案

#1


18  

two liner

两个班轮

NSMutableArray *uniqueArray = [NSMutableArray array];

[uniqueArray addObjectsFromArray:[[NSSet setWithArray:duplicateArray] allObjects]];

#2


2  

I've made a category on NSArray with this method in :

我已经使用此方法在NSArray上创建了一个类别:

- (NSArray *)arrayWithUniqueObjects {
    NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:[self count]];

    for (id item in self)
        if (NO == [newArray containsObject:item])
            [newArray addObject:item];

    return [NSArray arrayWithArray:newArray];
}

However, this is brute force and not very efficient, there's probably a better approach.

然而,这是蛮力而且效率不高,可能有更好的方法。

#3


2  

My solution:

我的解决方案

array1=[NSMutableArray arrayWithObjects:@"1",@"2",@"2",@"3",@"3",@"3",@"2",@"5",@"6",@"6",nil];
array2=[[NSMutableArray alloc]init];
for (id obj in array1) 
{
    if (![array2 containsObject:obj]) 
    {
        [array2 addObject: obj];
    }
}
NSLog(@"new array is %@",array2);

The output is: 1,2,3,5,6..... Hope it's help you. :)

输出是:1,2,3,5,6 .....希望它对你有所帮助。 :)

#4


0  

If the order of the values is not important, the easiest way is to create a set from the array:

如果值的顺序不重要,最简单的方法是从数组创建一个集合:

NSSet *set = [NSSet setWithArray:myArray];

It will only contain unique objects:

它只包含唯一的对象:

If the same object appears more than once in array, it is added only once to the returned set.

如果同一对象在数组中出现多次,则仅向返回的集添加一次。

#5


0  

If you are worried about the order, check this solution

如果您担心订单,请检查此解决方案

// iOS 5.0 and later
NSArray * newArray = [[NSOrderedSet orderedSetWithArray:oldArray] array];

#6


0  

NSSet approach is the best if you're not worried about the order of the objects

如果您不担心对象的顺序,NSSet方法是最好的

 uniquearray = [[NSSet setWithArray:yourarray] allObjects];

#1


18  

two liner

两个班轮

NSMutableArray *uniqueArray = [NSMutableArray array];

[uniqueArray addObjectsFromArray:[[NSSet setWithArray:duplicateArray] allObjects]];

#2


2  

I've made a category on NSArray with this method in :

我已经使用此方法在NSArray上创建了一个类别:

- (NSArray *)arrayWithUniqueObjects {
    NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:[self count]];

    for (id item in self)
        if (NO == [newArray containsObject:item])
            [newArray addObject:item];

    return [NSArray arrayWithArray:newArray];
}

However, this is brute force and not very efficient, there's probably a better approach.

然而,这是蛮力而且效率不高,可能有更好的方法。

#3


2  

My solution:

我的解决方案

array1=[NSMutableArray arrayWithObjects:@"1",@"2",@"2",@"3",@"3",@"3",@"2",@"5",@"6",@"6",nil];
array2=[[NSMutableArray alloc]init];
for (id obj in array1) 
{
    if (![array2 containsObject:obj]) 
    {
        [array2 addObject: obj];
    }
}
NSLog(@"new array is %@",array2);

The output is: 1,2,3,5,6..... Hope it's help you. :)

输出是:1,2,3,5,6 .....希望它对你有所帮助。 :)

#4


0  

If the order of the values is not important, the easiest way is to create a set from the array:

如果值的顺序不重要,最简单的方法是从数组创建一个集合:

NSSet *set = [NSSet setWithArray:myArray];

It will only contain unique objects:

它只包含唯一的对象:

If the same object appears more than once in array, it is added only once to the returned set.

如果同一对象在数组中出现多次,则仅向返回的集添加一次。

#5


0  

If you are worried about the order, check this solution

如果您担心订单,请检查此解决方案

// iOS 5.0 and later
NSArray * newArray = [[NSOrderedSet orderedSetWithArray:oldArray] array];

#6


0  

NSSet approach is the best if you're not worried about the order of the objects

如果您不担心对象的顺序,NSSet方法是最好的

 uniquearray = [[NSSet setWithArray:yourarray] allObjects];