从 NSMutable 数组中删除重复值 [英] remove duplicate valuse from NSMutable array

查看:36
本文介绍了从 NSMutable 数组中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的排序数组中删除任何重复项...我以为我可以使用我的另一个问题中的示例来完成它,但结果甚至无法正常工作.

I am trying to remove any duplicates from my sorted array... I thought I could do it using an example from another question of mine but it turns out to not even come close to working.

这是一个非常可怕的尝试,但这是我弄清楚如何比较 NSMutableArray 中单个项目(NSDictionary)的两个元素的唯一方法.

its a pretty horrible attempt, but its the only way i can figure out how to compare two elements of a single item(NSDictionary) in a NSMutableArray.

这是我的代码示例

我目前有一个排序数组

 NSArray *duplicatesRemovedArray = [sortedArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDictionary *dictA = a;
        NSDictionary *dictB = b;

        NSString *startYear1 = dictA[kStartYear];
        NSString *finishYear1 = dictA[kFinishYear];
        NSString *allYear1 = [NSString stringWithFormat:@"%@%@",startYear1, finishYear1];
        NSString *startYear2 = dictB[kStartYear];
        NSString *finishYear2 = dictB[kFinishYear];
        NSString *allYear2 = [NSString stringWithFormat:@"%@%@",startYear2, finishYear2];

        return [allYear1 compare:allYear2];

    }];

如果有人对如何创建我所追求的独特项目的 NSArray 有任何想法,如果您能给我任何建议,将不胜感激的代码示例

if anyone has any ideas on how to create an NSArray of unique items that is what I am after, if you could give me any suggestions, code examples that would be greatly appreciated

更新:我应该指出我的 NSDictionary 中有另一个元素身份证...

Update: I should point out that my NSDictionary has another element within it an ID...

所以我不能像你列出的例子那样只比较对象......这是我的疏忽.如此有效地对象看起来像这样

so I cannot just compare objects like in the examples you have listed... this was an oversight on my part. So effectivly the objects look like this

ID: 1234
STARTYEAR: 1999
FINISHYEAR: 2000

所以我需要一些帮助来删除 StartYear 和 FinishYear 与其他对象开始和结束年份匹配的对象.

so I need some help removing objects whos StartYear and FinishYear match other objects start and finish year.

推荐答案

因此,如果我正确理解您的问题,您希望以一种方式过滤您的数组,如果有多个项目具有相同的年份值(但可能不同ID),结果中只包含其中一个.

So if I understood your question correctly, you want to filter your array in a way that if there's more than one item with the same year values (but possibly different IDs), only one of them is contained in the result.

一个简单的方法是遍历数组,收集你在可变集合中已经看到的开始/结束年份,并且只在结果中包含开始/结束年份的组合您尚未添加的.

A simple way to do that would be to iterate over the array, collecting start/finish years that you've already seen in a mutable set, and only including items in the result if they have a combination of start/finish year that you haven't yet added.

NSArray *array = @[@{@"ID": @(1234), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2001)},
                   @{@"ID": @(1235), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2001)},
                   @{@"ID": @(1236), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2000)},
                   @{@"ID": @(1237), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2000)}];

NSMutableArray *duplicatesRemoved = [NSMutableArray array];
NSMutableSet *seenYears = [NSMutableSet set];
for (NSDictionary *item in array) {
    //Extract the part of the dictionary that you want to be unique:
    NSDictionary *yearDict = [item dictionaryWithValuesForKeys:@[@"STARTYEAR", @"FINISHYEAR"]];
    if ([seenYears containsObject:yearDict]) {
        continue;
    }
    [seenYears addObject:yearDict];
    [duplicatesRemoved addObject:item];
}
NSLog(@"%@", duplicatesRemoved);

在本例中,这将导致以下两项:

In this example, this would result in these two items:

{ FINISHYEAR = 2001; ID = 1234; STARTYEAR = 1999; }
{ FINISHYEAR = 2000; ID = 1236; STARTYEAR = 1999; }

这篇关于从 NSMutable 数组中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆