如何从数组中获取唯一值

编程入门 行业动态 更新时间:2024-10-28 18:22:21
本文介绍了如何从数组中获取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请注意 这是用于OSX,而​​不是iOS.

Please note this is for OSX, not for iOS.

我已经查看并尝试了其他SO问题中的一些解决方案,但是似乎没有一个对我有用,因此:

I have looked and tried some solutions in other SO questions, but none seem to work for me, hence this:

我想从数组中获得一组独特的年份.

I want to get a unique set of years out of an array.

我的代码是这样的:

NSMutableArray * unique = [NSMutableArray array]; NSMutableSet * processed = [NSMutableSet set]; for (yearString in yearStringArray) { if (![processed containsObject:yearString] == NO) { [unique addObject:yearString]; } [processed addObject:yearString]; } NSLog(@"unique: %@",unique ); NSLog(@"processed: %@",processed ); }]; The console returns: unique: ( ( 1941, 1942, 1943, 1945, 1948, 1948, 1948, 1949 ) processed: {( ( 1941, 1942, 1943, 1945, 1948, 1948, 1948, 1949 ) )}

我只想要一个1948年.我将不胜感激.

I only want one 1948. I would appreciate any help.

更新:感谢您的回答.请让他们来!!!

UPDATES: Thanks for the answers all. Keep em coming please!!!

这些是我现在尝试过的代码片段:

These are the code snips I've tried now:

1.

for (yearString in yearStringArray) { if ([processed containsObject:yearString] == NO) { [unique addObject:yearString]; } [processed addObject:yearString]; }

2.

NSString *yearString = [[NSString alloc] init]; NSArray *objectArray = [[NSArray alloc] init]; [objectArray = objects copy]; for (int j = 0; j <= objectArray.count; j++) { NSString *yearString = [objectArray valueForKey:@"year"]; [yearStringArray addObject:yearString]; } NSMutableArray * unique = [NSMutableArray array]; NSMutableSet * processed = [NSMutableSet set]; for (yearString in yearStringArray) { [unique addObject:yearString]; [processed addObject:yearString]; } NSArray *processed2 = [[NSSet setWithArray:unique] allObjects]; NSLog(@"unique: %@",unique ); NSLog(@"processed: %@",processed ); NSLog(@"processed2: %@",processed2 ); //[self setYears]; }];

3.

NSString *yearString = [[NSString alloc] init]; NSArray *objectArray = [[NSArray alloc] init]; [objectArray = objects copy]; for (int j = 0; j <= objectArray.count; j++) { yearString = [objectArray valueForKey:@"year"]; [yearStringArray addObject:yearString]; } NSArray *uniqueYears = [yearStringArray valueForKeyPath:@"@distinctUnionOfObjects.self"]; NSLog(@"uniqueYears: %@", uniqueYears); }]; }

仍然没有独特的年份.

推荐答案

一种简单的方法是从具有重复项的数组中创建一个NSSet.结果是一个集合,根据定义,该集合仅存储唯一对象.然后使用NSSet的-allObjects方法将NSSet转换回NSArray.唯一的缺点是,通过转换为NSSet,您将失去原始顺序.

An simple way is to create an NSSet from the array with duplicates. The result is a set which by definition stores only the unique objects. Then using NSSet's -allObjects method to convert the NSSet back to an NSArray. The only downside is you lose the original ordering by converting to an NSSet.

NSArray *uniqueArray = [[NSSet setWithArray:duplicateArray] allObjects];

如果您需要保留顺序并且可能需要10.7+,则可以使用NSOrderedSet.

If you need to preserve the ordering and can require 10.7+, you can use an NSOrderedSet.

NSArray *uniqueArray = [[NSOrderedSet orderedSetWithArray:duplicateArray] array];

感谢WWDC 2013 会议228-可可和可可粉中的隐藏宝石马特·汤普森/em>, 没有创建中间集的另一种方法.

Thanks to Mattt Thompson in the WWDC 2013 Session 228 - Hidden Gems in Cocoa and Cocoa Touch, there is another way without creating an intermediate set.

NSArray *uniqueArray = [duplicateArray valueForKeyPath:@"@distinctUnionOfObjects.self"]

更多推荐

如何从数组中获取唯一值

本文发布于:2023-10-23 04:28:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   唯一值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!