How to count duplicates values in NSArray?

后端 未结 3 1228
情书的邮戳
情书的邮戳 2021-02-02 03:08

Value of my NSArray includes the duplicates. I find the duplicates but now how can I find the no. they repeat?

3条回答
  •  广开言路
    2021-02-02 03:58

    Example:

    NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil];
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];
    
    for (id item in set) {
    
        NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
    }
    

提交回复
热议问题