问题描述
我的 NSMutableDictionary
包含四个 NSArray
及其各自的键。这些 NSArray
的大小为2,包含2D坐标。我想得到它们中最常见的坐标。例如,如果坐标对于三个数组是通用的,那么它将是我的第一选择。如何找到至少两个阵列的任何坐标?
My NSMutableDictionary
contains four NSArray
s with their respective keys. These NSArray
s are of size 2 and contain 2D coordinates. I want to get the most common coordinate among them. For example, If a coordinate is common to three arrays, it would be my first choice. How can I find that any coordinate is common to at least two arrays?
推荐答案
基本上你想要找到具有最大出现次数的坐标对。
所以你可以创建一个包含所有坐标的数组并找到它的模式。
Basically you want to find the pair of coordinates that has max occurences.So you can create an array of all coordinates and find its mode.
NSMutableArray *allCoordinates = [NSMutableArray new];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([key isEqualToString:@"arrayKey1"] || [key isEqualToString:@"arrayKey2"]) {
NSArray *coordinates = (NSArray *)obj;
[coordinates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[allCoordinates addObject:coordinates];
}];
}
}];
现在,你需要编写一个自定义方法来查找坐标数组的模式(附加一个频率条件> = 3)。
Now, U need to write a custom method to find mode of an array of coordinates (with an additional condition of frequency being >=3).
这篇关于如何在NSMutableDictionary中比较NSArray的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!