我想這個代碼生成一個隨機數並將數組列表保存在一個數組中,然後我試圖從列表中逐個刪除那些出現一次的數字,例如 1,5,9,4, 3,7,6,10,11,8,2是整數列表現在9出現一次,現在我不需要再次9 ..這是我的隨機非重複數字數組的代碼。如何從非重複數組中刪除數字?
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
BOOL record = NO;
int x;
for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
{
x = arc4random() % 10;//generating random number
if(i==0)//for first time
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
else
{
for (int j=0; j<= [storeArray count]-1; j++)
{
if (x ==[[storeArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
{
record = NO;
}
else
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
}
}
它只是得到數字,數字重複! – Steve
您添加 .h文件 BOOL isSame; NSMutableArray * countArray; NSInteger randomNumber; .m文件 countArray = [[NSMutableArray alloc] init]; –
其重複的先生! – Steve