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

编程入门 行业动态 更新时间:2024-10-27 20:31:23
本文介绍了如何在数组中查找重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am working on SQLite and I have written a query which returns me two arrays ItemsArray and CustomersIDArray as:

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(计数),Fan = 2(计数)和Delux = 1; 和结果数组,

I want result like that Off White = 2 (count) , Fan = 2 (count) and Delux = 1; and the Resultant Array,

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

实际上我想要第一个数组中的重复次数,但CustomerArray的值必须不相同。 请帮我完成逻辑或代码。

Actually I want the count of repetition in first array but the value must not same for CustomerArray. Please help me through logic or code.

推荐答案

-(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,而indexArray位于index:1。

I just set up -getArrayInfo to test it out. Works fine. As you can see, the array you want to display will be at index:0, and the countArray at index:1.

更多推荐

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

本文发布于:2023-11-22 00:54:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!