Remove duplicate from nsmutablearray containing nsdictionary

后端 未结 2 1135
余生分开走
余生分开走 2021-01-15 04:55

I want to remove duplicates from nsmutablearray.

Array Structure :-

(
    {
        \"act_end_date\" = \"\";
        \"act_entry_date\"          


        
2条回答
  •  借酒劲吻你
    2021-01-15 05:29

    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;
    }
    

提交回复
热议问题