Since the order of the int
s does not matter (they are random, anyway) replacing NSMutableArray
container with NSMutableSet
will let you avoid duplicates. All you need to do now is check the size of the container, and stop when you reach the desired size of four:
NSMutableSet *theSequence = [NSMutableSet set];
do {
int r = arc4random()%12 + 1;
[theSequence addObject:[NSNumber numberWithInteger:r]];
} while (theSequence.count != difficultyLevel);
Note: If for some reason the insertion order is important, you can use NSMutableOrderedSet
.