Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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
Iphone 如何在数组中查找重复值?_Iphone_Ios_Xcode4.2_Ios Simulator - Fatal编程技术网

Iphone 如何在数组中查找重复值?

Iphone 如何在数组中查找重复值?,iphone,ios,xcode4.2,ios-simulator,Iphone,Ios,Xcode4.2,Ios Simulator,我正在处理SQLite,我编写了一个查询,返回两个数组ItemsArray和CustomerSiArray,如下所示: ItemsArray Element at Index 0 = Off White, Element at Index 1 = Fan, Element at Index 2 = Off White, Element at Index 3 = Delux, Element at Index 4 = Fan CustomerIDArray Element at Index 0

我正在处理SQLite,我编写了一个查询,返回两个数组ItemsArray和CustomerSiArray,如下所示:

ItemsArray
Element at Index 0 = Off White,
Element at Index 1 = Fan,
Element at Index 2 = Off White,
Element at Index 3 = Delux,
Element at Index 4 = Fan

CustomerIDArray 
Element at Index 0 = 1,
Element at Index 1 = 2,
Element at Index 2 = 2,
Element at Index 3 = 3,
Element at Index 4 = 4
我想要这样的结果:灰白色=2(计数),扇形=2(计数)和消光=1; 以及生成的数组

Result Array 
Element at Index 0 = Off White,
Element at Index 1 = Fan,
Element at Index 2 = Delux
实际上,我想要第一个数组中的重复计数,但CustomerArray的值不能相同。 请通过逻辑或代码帮助我。

尝试以下操作:

NSArray *copy = [ItemsArray copy];

NSInteger index = [copy count] - 1;

for (id object in [copy reverseObjectEnumerator]) {

    if ([ItemsArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
        [ItemsArray removeObjectAtIndex:index];
    }
    index--;
}

使用
NSCountedSet
如下所示

NSMutableArray *ary_res = [[NSMutableArray alloc] init];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"11",@"13",@"34",@"9",@"13",@"34",@"9",@"2",nil];
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:array];
    for(id name in set)
    {
        if([set countForObject:name]==2)
            [ary_res addObject:name];
    }
    //
    NSLog(@"%@",ary_res);
-(NSMutableArray*)getCountAndRemoveMultiples:(NSMutableArray*)数组{
NSMutableArray*newArray=[[NSMutableArray alloc]initWithArray:(NSArray*)数组];
NSMUTABLEARRY*countArray=[NSMUTABLEARRY new];
int countInt=1;
对于(int i=0;i

我刚刚设置了-getArrayInfo来测试它。很好。如您所见,要显示的数组将位于索引0处,而countArray位于索引1处。

请尝试此问题提供的解决方案:()@INoob亲爱的,我不知道如何使用NSCountedSet。。您能告诉我吗?在这里尝试一下答案:@iNoob如何计算重复元素计数:(@AmirIphone查看我的答案我更改了它,它对我有效。它的更新项目数组。但我还需要重复项目的计数。我建议不要使用名称副本到数组中,因为人们可能会与您在中使用的消息副本混淆:NSArray*copy=[ItemsArray copy];我需要在数组中生成结果,如果两次,则再次保存2项,如果三次,则保存3项,如果一次,则保存1项。是的……但我还想知道它在数组中出现了多少次这真是太好了,我根据自己提交了一些更改……竖起大拇指:)我还有一个问题,你能帮我吗?当然可以--发布一个问题,并在这里留下一个链接作为评论。上面的回答在逻辑上是不正确的。请使用此输入尝试NSMutableArray*myArray=[[NSMutableArray alloc]initWithObjects:@“Off White”、@“Fan”、@“Off White”、@“Deluxe”、@“Fan”、@“Fan”、@“Fan”、nil];。您可以看到“Fan”的计数是3,但实际上是4。若要更正此问题,您需要在从数组中删除元素后减小j(否则数组中的下一个字符串将不计算)。编辑了答案
-(NSMutableArray *)getCountAndRemoveMultiples:(NSMutableArray *)array{

    NSMutableArray *newArray = [[NSMutableArray alloc]initWithArray:(NSArray *)array];
    NSMutableArray *countArray = [NSMutableArray new];
    int countInt = 1;
    for (int i = 0; i < newArray.count; ++i) {
        NSString *string = [newArray objectAtIndex:i];
        for (int j = i+1; j < newArray.count; ++j) {
            if ([string isEqualToString:[newArray objectAtIndex:j]]) {
                [newArray removeObjectAtIndex:j];
                countInt++;
            }
        }
        [countArray addObject:[NSNumber numberWithInt:countInt]];
        countInt = 1;
    }
    NSMutableArray *finalArray = [[NSMutableArray alloc] initWithObjects:newArray, countArray, nil];
    NSLog(@"%@", finalArray);
    return finalArray;

}
- (IBAction)getArrayInfo:(id)sender {
    NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"Off White", @"Fan", @"Off White", @"Deluxe", @"Fan", nil];
    NSMutableArray *godArray = [self getCountAndRemoveMultiples:myArray];
    NSLog(@"Array from this end = %@", godArray);
}