前端之家收集整理的这篇文章主要介绍了
Swift学习笔记-判断字符出现的次数,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/** 判断字符出现的次数 */
let str = "asfkj294sjdkflskadjw8tghjhf8t8as"
var status : [Character : Int] = [:]
for ch in str
{
var num = status[ch]
if num != nil
{
status[ch] = num! + 1
}
else
{
status[ch] = 1
}
}
println(status)
var maxOccurs = 0
for occurs in Array(status.values)
{
if occurs > maxOccurs
{
maxOccurs = occurs
}
}
for (ch,occurs) in status
{
if occurs == maxOccurs
{
println("出现最多的字符 \(ch),出现的次数\(occurs)")
}
}
原文链接:https://www.f2er.com/swift/327043.html