【问题标题】:How to Find Duplicate Values in Arrays?如何在数组中查找重复值?
【发布时间】:2012-05-04 01:44:15
【问题描述】:

我正在研究 SQLite,我编写了一个查询,它返回两个数组 ItemsArray 和 CustomersIDArray:

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

我想要这样的结果 Off White = 2 (count) , Fan = 2 (count) and Delux = 1; 和结果数组,

Result Array 
Element at Index 0 = Off White,
Element at Index 1 = Fan,
Element at Index 2 = Delux

实际上我想要第一个数组中的重复计数,但 CustomerArray 的值不能相同。 请通过逻辑或代码帮助我。

【问题讨论】:

标签: iphone ios xcode4.2 ios-simulator


【解决方案1】:

使用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);

【讨论】:

  • 如果两次,我需要数组中的结果,然后再次保存 2,如果三次,则保存 3,如果一次然后 1。
  • 是的......但也想知道它在数组中出现了多少次
【解决方案2】:
-(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);
}

我刚刚设置了 -getArrayInfo 来测试它。工作正常。如您所见,您要显示的数组将在 index:0 处,而 countArray 在 index:1 处。

【讨论】:

  • 它的工作真的很不错,我根据我的意见进行了一些更改...竖起大拇指:)
  • 我还有一个问题你能帮我吗?
  • 当然——发布一个问题,并在此处留下一个链接作为评论。
  • 上述答案在逻辑上不正确。试试这个输入 NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"Off White", @"Fan", @"Fan", @"Off White", @"Deluxe", @"Fan", @"Fan ", 无]; .你可以看到“粉丝”的数量是 3,但实际上是 4。要更正此问题,您需要在从数组中删除元素后减少 j (否则不会评估数组中的下一个字符串)。编辑了答案
【解决方案3】:

试试这个:

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--;
}

【讨论】:

  • 我改变了它,它对我有用。它的更新项目数组。但我也想要重复项目的计数。
  • 我建议不要使用名称复制到数组,因为人们可能会混淆您也在其中使用的消息复制:NSArray *copy = [ItemsArray copy];
猜你喜欢
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2020-02-15
  • 1970-01-01
相关资源
最近更新 更多