Remove duplicate from nsmutablearray containing nsdictionary

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-21 10:16:07

问题


I want to remove duplicates from nsmutablearray.

Array Structure :-

(
    {
        "act_end_date" = "";
        "act_entry_date" = "13/11/2014";
        "act_recurrrance_type" = Daily;
        "act_start_date" = "2014-11-13";
        "row_id" = 0;
        "act_id" = 2;
    }
    {
        "act_end_date" = "";
        "act_entry_date" = "13/11/2014";
        "act_recurrrance_type" = Daily;
        "act_start_date" = "2014-11-13";
        "row_id" = 1;
        "act_id" = 2;
    }
    {
        "act_end_date" = "";
        "act_entry_date" = "16/11/2014";
        "act_recurrrance_type" = Daily1;
        "act_start_date" = "2014-11-15";
        "row_id" = 2;
        "act_id" = 3;
    }
)

What I want is to check where array contain any dictionary which have same act_id. If yes then remove 1st dictionary. i.e the expected result for the above arrayis :-

(
        {
            "act_end_date" = "";
            "act_entry_date" = "13/11/2014";
            "act_recurrrance_type" = Daily;
            "act_start_date" = "2014-11-13";
            "row_id" = 1;
            "act_id" = 2;
        }
        {
            "act_end_date" = "";
            "act_entry_date" = "16/11/2014";
            "act_recurrrance_type" = Daily1;
            "act_start_date" = "2014-11-15";
            "row_id" = 2;
            "act_id" = 3;
        }
    )

Is their any way? Is it possible by using NSPREDICATE?

I Tries to do it in below way :-

  NSMutableArray *filteredArray = [NSMutableArray array];

    for (NSMutableDictionary* E1 in Event_Array)
    {

        BOOL hasDuplicate = [[filteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"act_id == %@", [E1 objectForKey:@"act_id"]]] count] > 0;

        if (!hasDuplicate)
        {
            [filteredArray addObject:E1];
        }
    }

For it returns array :---

(
    {
        "act_end_date" = "";
        "act_entry_date" = "13/11/2014";
        "act_recurrrance_type" = Daily;
        "act_start_date" = "2014-11-13";
        "row_id" = 0;
        "act_id" = 2;
    }
)

What i want is

(
    {
        "act_end_date" = "";
        "act_entry_date" = "13/11/2014";
        "act_recurrrance_type" = Daily;
        "act_start_date" = "2014-11-13";
        "row_id" = 1;
        "act_id" = 2;
    }
)

回答1:


Assuming you want the latest object to be in the list.

Solution 1: (Simple)

Reverse your array

NSMutableArray *filteredArray = [NSMutableArray array];
for (int i = Event_Array.count-1; i>= 0; i--)
{
    NSMutableDictionary* E1 = [Event_Array objectAtIndex:i];
    BOOL hasDuplicate = [[filteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"act_id == %@", [E1 objectForKey:@"act_id"]]] count] > 0;

    if (!hasDuplicate)
    {
        [filteredArray addObject:E1];
    }
}

Solution 2:

Use NSMutableDictionary instead of array.

//Keys are act_id(s)
"2" = {
    "act_end_date" = "";
    "act_entry_date" = "13/11/2014";
    "act_recurrrance_type" = Daily;
    "act_start_date" = "2014-11-13";
    "row_id" = 1;
    "act_id" = 2;
};
"3" = {
    "act_end_date" = "";
    "act_entry_date" = "16/11/2014";
    "act_recurrrance_type" = Daily1;
    "act_start_date" = "2014-11-15";
    "row_id" = 2;
    "act_id" = 3;
}



回答2:


    NSMutableSet *keysSet = [[NSMutableSet alloc] init];
    NSMutableArray *filteredArray = [[NSMutableArray alloc]init];
    for (NSDictionary *msg in germanMakes) {
    NSString *key = [NSString stringWithFormat:@"%@", msg[@"act_id"]];
    if (![keysSet containsObject:key]) {
        [filteredArray addObject:msg];
        [keysSet addObject:key];
    }
    }
    NSLog(@"filteredResults %@ keyset%@",filteredArray , keysSet);

germanMakes is your initial array, keysSet contains unique act_id (e.g. 1,2,3,4..), filteredArray is your array filtered by act_id. Keep in mind this does not get sorted ascending, but that part will be easy.



来源:https://stackoverflow.com/questions/26904295/remove-duplicate-from-nsmutablearray-containing-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!