查找 NSArray 中重复项的索引

发布于 2024-09-24 09:55:29 字数 939 浏览 0 评论 0原文

我有一个数组,就像

[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];

我需要查找重复项和非重复元素的索引。 如何做到这一点......给出一些示例代码或逻辑...... 提前感谢

我使用 Objective C...

    NSArray *myWords = [string componentsSeparatedByString:@"class=\""];

    int count_var=[myWords count];


    tmp1=@"";
    for(int i=1;i<count_var;i++)
    {

        str=[NSString stringWithFormat:@"\n%@",[myWords objectAtIndex:i]];
        class=[str componentsSeparatedByString:@"\""];

        NSString *tmp=[NSString stringWithFormat:@"%@",[class objectAtIndex:0]];
        tmp1=[[NSString stringWithFormat:@"%@",tmp1] stringByAppendingString:[NSString stringWithFormat:@"%@",tmp]];
    } 
    t1.editable=NO;
    t1.text=tmp1;
NSArray *tempo=[[NSArray alloc]init];
tempo=[tmp1 componentsSeparatedByString:@"\n"];

tempCount=[tempo count];

这是我的示例代码...其中数组速度包含该数组中的所有对象,我想获取重复字符串的索引≥。

i have an array like

[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];

i need to find indexes of duplicates and also non duplicate elements .
how to do this...........give some sample code or logic......
thanks in advance

iam using objective c.....

    NSArray *myWords = [string componentsSeparatedByString:@"class=\""];

    int count_var=[myWords count];


    tmp1=@"";
    for(int i=1;i<count_var;i++)
    {

        str=[NSString stringWithFormat:@"\n%@",[myWords objectAtIndex:i]];
        class=[str componentsSeparatedByString:@"\""];

        NSString *tmp=[NSString stringWithFormat:@"%@",[class objectAtIndex:0]];
        tmp1=[[NSString stringWithFormat:@"%@",tmp1] stringByAppendingString:[NSString stringWithFormat:@"%@",tmp]];
    } 
    t1.editable=NO;
    t1.text=tmp1;
NSArray *tempo=[[NSArray alloc]init];
tempo=[tmp1 componentsSeparatedByString:@"\n"];

tempCount=[tempo count];

this is my sample code...in this the array tempo contains all objects from that array i want to get index of duplicate strings≥.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-10-01 09:55:29

您可以构建一个将对象映射到索引集的字典。对于每个索引集,-count1 表示没有重复项,> 1 表示存在重复项。

NSArray *arr = [NSArray arrayWithObjects:...];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for (NSUInteger i=0; i<[arr count]; ++i) {
    id obj = [arr objectAtIndex:i];
    NSMutableIndexSet *ids = [dict objectForKey:obj];
    if (!ids) {
        ids = [NSMutableIndexSet indexSet];
        [dict setObject:ids forKey:obj];
    }
    [ids addIndex:i];
}

NSLog(@"%@", dict);

You could build a dictionary mapping the objects to index sets. For every index set, a -count of 1 means no duplicates, > 1 means there are duplicates.

NSArray *arr = [NSArray arrayWithObjects:...];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for (NSUInteger i=0; i<[arr count]; ++i) {
    id obj = [arr objectAtIndex:i];
    NSMutableIndexSet *ids = [dict objectForKey:obj];
    if (!ids) {
        ids = [NSMutableIndexSet indexSet];
        [dict setObject:ids forKey:obj];
    }
    [ids addIndex:i];
}

NSLog(@"%@", dict);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文