-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path01_unique.swift
34 lines (27 loc) · 889 Bytes
/
01_unique.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Are the letters unique?
// Write a function that accepts a string as its only param and returns
// true if the string has only unique letters taking letter case into account.
func isUnique(_ input: String) -> Bool {
var checkLetter = [Character]()
for letter in input.characters {
if checkLetter.contains(letter) {
return false
}
checkLetter.append(letter)
}
return true
}
func isUnique2(_ input: String) -> Bool {
return Set(input.characters).count == input.characters.count
}
// call function
print(isUnique("AaBbCc")) // true
print(isUnique("Hello, world!")) // false
print(isUnique2("AaBbCc")) // true
print(isUnique2("Hello, world!")) // false
// Swift 4
func isUnique3(_ input: String) -> Bool {
return Set(input).count == input.count
}
print(isUnique3("AaBbCc")) // true
print(isUnique3("Hello, world!")) // false