Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 数组-查找对象连续重复的次数_Objective C_Arrays_Algorithm_Sorting - Fatal编程技术网

Objective c 数组-查找对象连续重复的次数

Objective c 数组-查找对象连续重复的次数,objective-c,arrays,algorithm,sorting,Objective C,Arrays,Algorithm,Sorting,我的数组对象如下所示: 10,10,10 20,23,14 10,10,10 10,10,10 10,10,10 32,23,42 32,23,42 10,10,10 32,23,23 32,23,23 如何遍历这个数组,找出同一个对象按顺序重复了多少次,然后添加一个,以及重复的次数 然后使用以下对象保存一个新数组: 10,10,10,1 20,23,14,1 10,10,10,3 32,23,42,2 10,10,10,1 32,23,23,2 任何帮助都将不胜感激 谢谢 将每三个整数分解

我的数组对象如下所示:

10,10,10
20,23,14
10,10,10
10,10,10
10,10,10
32,23,42
32,23,42
10,10,10
32,23,23
32,23,23
如何遍历这个数组,找出同一个对象按顺序重复了多少次,然后添加一个,以及重复的次数

然后使用以下对象保存一个新数组:

10,10,10,1
20,23,14,1
10,10,10,3
32,23,42,2
10,10,10,1
32,23,23,2
任何帮助都将不胜感激


谢谢

将每三个整数分解为自己的数组(确保它们是字符串)

然后遍历这些数组中的每一个,并输入到NSMutableDictionary中,键是字符串(您的数字),值是计数器(如果只看到一次,则添加1,等等)

保留指向最高键的指针(如果newCount>highestCountPointer,则highestCountPointer=newCount)

在迭代结束时,将最高计数所指向的数字添加到数组的末尾。

尝试以下操作:

NSMutableArray *outArray = [[NSMutableArray alloc] init];
for (NSUInteger j = 0; j < [theArray count]; j++) {
    id object = [theArray objectAtIndex:j];
    NSUInteger repeats = 1;
    while (j + 1 < [theArray count] && [[theArray objectAtIndex:j + 1] isEqual:object]) {
        j++;
        repeats++;
    }
    [outArray addObject:object];
    [outArray addObject:[NSNumber numberWithUnsignedInteger:repeats]];
}
return outArray;
NSMutableArray*outArray=[[NSMutableArray alloc]init];
对于(整数j=0;j<[阵列计数];j++){
id object=[theArray objectAtIndex:j];
整数重复次数=1;
而(j+1<[阵列计数]&&&[[阵列对象索引:j+1]相等:对象]){
j++;
重复++;
}
[outArray addObject:object];
[outArray addObject:[NSNumber numberWithUnsignedInteger:repeats];
}
返回数组;

如果输入数组是可变的,也可以在适当的位置执行此操作。我把它留给读者作为练习。

我不是一个客观的C程序员,所以请原谅任何语言错误。类似于以下内容的内容应该可以完成这项工作:

NSMutableArray *result = [[NSMutableArray alloc] init];
id pending = nil;
NSUInteger count = 0;
for (NSUInteger i = 0; i < [theArray count]; i++) {
    id object = [theArray objectAtIndex:i];
    if ([object isEqual:pending]) {
        count++;
    } else {
        if (pending != nil) {
            [result addObject:[NSString stringWithFormat:@"%@,%d", pending, count]];
        }
        pending = object;
        count = 1;
    }
}
if (pending != nil) {
    [result addObject:[NSString stringWithFormat:@"%@,%d", pending, count]];
}
NSMutableArray*result=[[NSMutableArray alloc]init];
待决id=零;
整数计数=0;
对于(整数i=0;i<[阵列计数];i++){
id object=[theArray objectAtIndex:i];
if([对象相等:挂起]){
计数++;
}否则{
如果(待定!=无){
[结果添加对象:[NSString stringWithFormat:@“%@,%d”,挂起,计数];
}
未决=对象;
计数=1;
}
}
如果(待定!=无){
[结果添加对象:[NSString stringWithFormat:@“%@,%d”,挂起,计数];
}

只需从命令行运行“uniq-c:)

为什么
10,10,10
的第一个和第三个实例获取
,1
,而第二个实例获取
,3
?还有,这些对象是字符串吗?因为我只关心连续的重复。而不是对象的总出现次数。为什么大拇指朝下?!“同一对象按顺序重复,然后添加一个,重复次数”是什么意思?请编辑该问题以解决其问题。当这起作用时,您的运行时为O(J*N)(虽然此处N=3,但可能更糟)O(J*N),J为任何常数时仍然是O(N)。任何涉及到检查数组的每个元素的解决方案都至少是O(N)。对,但是尽管这个例子是常数,但是研究这个算法的人可以将它应用到一个变量J@waf我仍然不明白如何改进它。当然,任何涉及扫描数组并将每个元素与数组中的下一个元素进行比较的算法的运行时间都将与数组长度乘以比较操作的平均运行时间成正比。OP可以通过进行指针比较来缩短比较时间,如果这恰好合适的话。(我还想指出,我认为您假设
isEqual
的运行时间与输入字符串的长度成正比,但平均情况可能更快。)@waf-运行时间不是O(J*N);它是O(J)(假设在符号中,J是原始数组的长度,N是最大重复计数)。内循环体的每次迭代都会将外循环的迭代次数减少一次,因此它们处于完美的平衡状态。OP不是寻找唯一的次数,而是一种游程长度编码。重新格式化“uniq-c”的结果将得到“游程长度编码”(提示:使用sed或tr)OP只需要一种游程长度编码,不是具有计数的唯一值的列表。