-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindThirdMax.swift
52 lines (44 loc) · 1.15 KB
/
FindThirdMax.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
41
42
43
44
45
46
47
48
49
50
51
52
import Foundation
func FindThirdMaximum(array: [Int]) -> Int {
var max1: Int?
var max2: Int?
var max3: Int?
for value in array {
if value == max1 || value == max2 || value == max3 {
continue
}
if max1 == nil {
max1 = value
} else if max2 == nil {
if value > max1! {
max2 = max1!
max1 = value
} else {
max2 = value
}
} else if max3 == nil {
if value > max1! {
max3 = max2!
max2 = max1!
max1 = value
} else if value > max2! {
max3 = max2!
max2 = value
} else {
max3 = value
}
} else if value > max1! {
max3 = max2
max2 = max1
max1 = value
} else if value > max2! {
max3 = max2
max2 = value
} else if value > max3! {
max3 = value
}
}
if max3 == nil {return max1!}
if max2 == nil {return max1!}
return max3!
}