iPhone SDK:如何删除NSArray中的重复项 [英] iPhone sdk: how to delete duplicates in the NSArray
问题描述
我的NSArray包含这样的重复项(我必须删除重复项)
Hi My NSArray contains duplicates like this(i have to delete the duplicates)
标题:父亲节
标题:父亲节
标题:父亲节
标题:选举日
标题:选举日
标题:选举日
标题:复活节
标题:复活节
标题:地球日
标题:地球日
标题:地球日
标题:Cinco de Mayo
titles: Cinco de Mayo
标题:Cinco de Mayo
titles: Cinco de Mayo
标题:Cinco de Mayo
titles: Cinco de Mayo
标题:平安夜
标题:平安夜
标题:平安夜
标题:圣诞节 标题:圣诞节 标题:圣诞节
titles: Christmas titles: Christmas titles: Christmas
我只保留一个名字,不需要其他重复的名字. 我如何做到这一点,请为我提供清晰明确的指导,以解决此问题. 谢谢你.
I have keep one name only and other duplicates are not wanted. how i have do this please guide me with bit clear explanation, to solve this issue. Thank you.
推荐答案
如果您关心排序,则可以创建一个新的可变数组,遍历旧数组中的每个对象,仅在尚不存在时才添加它:
If you care about ordering, you can create a new mutable array, loop through each object in the old array and only add it if it doesn't already exist:
NSMutableArray *uniqueItems = [NSMutableArray array];
for (id item in allItems)
if (![uniqueItems containsObject:item])
[uniqueItems addObject:item];
如果没有,使用一个集合要简单得多:
If not, it's much simpler to just use a set:
NSSet *uniqueItems = [NSSet setWithArray:allItems];
这篇关于iPhone SDK:如何删除NSArray中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!