您的当前位置:首页正文

Swift-字符串去重

2024-12-21 来源:二三四教育网

题目:删除字符串中重复出现的字符,输入“FlyElephant”,返回“FlyEephant”.
核心代码:
<pre><code>`

func deleteDuplicationChar(origin:String) -> String {
    var dict:[String:Int] = [:]
    var result:String = ""
    for i in 0..<origin.characters.count {
        let char:String = origin[i]
        if dict[char] == nil {
            result += char
            dict[char] = 1
        }             
    }
    return result
}`</code></pre>

测试代码:
<pre><code>`

var simple:String = hashSearch.deleteDuplicationChar(origin: "FlyElephant")
print("FlyElephant-去除重复之后的字符串---(simple)")`</code></pre>

显示全文