如何检查数组中数字的重复? [英] How to check repetition of numbers in an array?
本文介绍了如何检查数组中数字的重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我生成随机数并将其存储在数组中
I generate random numbers and store them in an array:
int RandomNumber = arc4random() % 12;
[NSMutablearray *Number addObject:[NSNumber numberWithInt:RandomNumber]];
现在,我要确保不会再次随机创建相同的数字.谁能告诉我如何使用示例代码.
Now I want to make sure the same number is not created randomly again. Can any one please tell me how to do it with sample code.
推荐答案
在生成数字时,可以使用 NSMutableSet
而不是数组.以下代码将创建一个包含10个唯一随机数的数组:
You can use an NSMutableSet
instead of an array while generating the numbers.
The following code will create an array of 10 unique random numbers:
NSMutableSet *aSet = [NSMutableSet setWithCapacity:10];
while([aSet count]<=10){
int Randnum = arc4random() % 12;
[aSet addObject:[NSNumber numberWithInt:Randnum]];
}
NSArray *arrayOfUniqueRandomNumbers = [aSet allObjects];
这篇关于如何检查数组中数字的重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文