我想知道是否有一种简单的方法来检查六个不同随机数中的重复项。我想检查6个随机数中的4个是否相同以创建if语句。
例如;
let firstRandomNumber = arc4random_uniform(3) + 1
let secondRandomNumber = arc4random_uniform(3) + 1
let thirdRandomNumber = arc4random_uniform(3) + 1
let fourthRandomNumber = arc4random_uniform(3) + 1
let fifthRandomNumber = arc4random_uniform(3) + 1
let sixthRandomNumber = arc4random_uniform(3) + 1
如果有四个或更多相同的随机数,我想执行和操作(例如3, 3, 3, 3, 2, 1
。)任何人都知道如何?
答案 0 :(得分:0)
这个怎么样?
hostname.com - - [01/Jan/2016:00:00:00 +0000] "GET /stuff HTTP/1.1" 200 1046 "http://mywebsite.com" "Mozilla/5.0 (Windows NT 6.1)"
答案 1 :(得分:0)
这是一个稍微简短的解决方案:
let numbers = [3, 3, 2, 3, 2, 3]
let counts = numbers.map({ (N) in numbers.filter({ $0 == N }).count })
let fourOrMore = counts.contains({ $0 >= 4 })
if fourOrMore { print(" there are more than four identical values") }
甚至更短的一个:
let numbers = [3, 3, 2, 3, 2, 3]
if numbers.contains({ (N) in numbers.filter({$0 == N}).count >= 4 })
{ print(" there are more than four identical values") }