0

In my code I generate six random numbers between 1-5.

RandomNumber1 = arc4random_uniform(5) + 1
RandomNumber2 = arc4random_uniform(5) + 1
RandomNumber3 = arc4random_uniform(5) + 1
RandomNumber4 = arc4random_uniform(5) + 1
RandomNumber5 = arc4random_uniform(5) + 1
RandomNumber6 = arc4random_uniform(5) + 1

With these numbers, I want to check how many duplicates there are. Two scenarios: I want to execute one code if there are 5 different numbers, and another code if there isn't. Example;

1, 2, 3, 4, 5, 5

Here are 5 different numbers (only one pair)

1, 1, 2, 2, 3, 4

Here are not 5 different numbers (two pairs)

Rappe Stegarn
  • 347
  • 1
  • 4
  • 10

1 Answers1

3

Create a Set and use that to count the unique values:

if Set([RandomNumber1,RandomNumber2, RandomNumber3, RandomNumber4, RandomNumber5, RandomNumber6]).count == 5 {
    print("five")
} else {
    print("not five")
}
vacawama
  • 150,663
  • 30
  • 266
  • 294