-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
40 lines (29 loc) · 981 Bytes
/
Contents.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
35
36
37
38
39
40
// MARK: - MOST COMMON NAME IN ARRAY
func mostCommonNameInArray(array: [String]) -> String{
var nameCountDictionary = [String: Int]()
// find names and its count
for name in array {
if let count = nameCountDictionary[name]{
nameCountDictionary[name] = count + 1
}
else{
nameCountDictionary[name] = 1
}
}
var mostCommonName = ""
// find most common name
for key in nameCountDictionary.keys{
if mostCommonName == ""{
mostCommonName = key
}else{
var count = nameCountDictionary[key]
if count > nameCountDictionary[mostCommonName]{
mostCommonName = key
}
}
print("\(key) : \(nameCountDictionary[key]!)")
}
return mostCommonName
}
// MARK: - PLAYGROUND
mostCommonNameInArray(array: ["Bob", "Sally", "Bob", "Sam", "Micheal", "Bob", "Micheal"])