nsstring - Objective-C: How to find the most common string in an array? -
i have array of strings online database trying determine commonly used word. values inside arrays vary want check common words of whatever collection or words i'm using. if theoretically had array of following...
nsarray *stringarray = [nsarray arraywithobjects:@"duck", @"duck", @"duck", @"duck", @"goose"]; how iterate through array determine common string, "duck"?
simplest way nscountedset:
nscountedset* stringset = [[nscountedset alloc] initwitharray:strings]; nsstring* mostcommon = nil; nsuinteger highestcount = 0; for(nsstring* string in stringset) { nsuinteger count = [stringset countforobject:string]; if(count > highestcount) { highestcount = count; mostcommon = string; } }
Comments
Post a Comment