Objective-C: How to find the most common string in an array?

后端 未结 3 1532
礼貌的吻别
礼貌的吻别 2021-01-21 08:02

I have an array of strings from an online database that I trying to determine the most commonly used word. The values inside the arrays will vary but I want to check the most co

3条回答
  •  耶瑟儿~
    2021-01-21 08:17

    Try using NSPredicate.

    NSUInteger count=0;
    NSString *mostCommonStr;
    for(NSString *strValue in stringArray) {
       NSUInteger countStr=[[stringArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self MATCHES[CD] %@, strValue]]count];
       if(countStr > count) {
           count=countStr;
           mostCommonStr=strValue;
       }
    }
    NSLog(@"The most commonstr is %@",mostCommonStr);
    

提交回复
热议问题