forked from codecat15/Youtube-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
111 lines (77 loc) · 2.46 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Foundation
/**
I hope the video was helpful, please feel free to reach out for any issues or any if you have any machine round question then feel free to share it with me I'll try and add them to the series
Based on the feedback of the video, I plan to continue this series, if you like to see such content in the future then please do like the video and add your comments in the video
Regards,
Ravi
*/
// Question # 1: remove duplicate from array
var arrayWithDuplicates : [Int] = [3,3,4,7,15,2,1,2,7,10,12,11,10,15,18,15,20,11]
var charArray : [Character] = ["a","a","c","b","b","d","e","e"]
extension Array where Element : Equatable {
func removeDuplicate() -> [Element] {
guard !self.isEmpty else {return []}
var temp : [Element] = []
self.forEach { item in
if(!temp.contains(item)) {
temp.append(item)
}
}
return temp
}
}
let result = arrayWithDuplicates.removeDuplicate()
print(result)
//let result = Set(arrayWithDuplicates)
//print(result)
//func removeDuplicate(arr: Array<Int>) -> Array<Int> {
//
// guard !arr.isEmpty else {return []}
//
//
// var temp : [Int] = []
//
// arr.forEach { item in
// if(!temp.contains(item)) {
// temp.append(item)
// }
// }
//
// return temp
//}
// Question # 2: create an array extension for only Int types
//extension Array where Element == Int {
//
// func removeDuplicate() -> [Element] {
//
// guard !self.isEmpty else {return []}
//
// var temp : [Element] = []
//
// self.forEach { item in
// if(!temp.contains(item)) {
// temp.append(item)
// }
// }
//
// return temp
// }
//}
// Question #3: Get all the capital characters from a string
var str = "Hello World"
func filterCapitalCharacters(fromString input: String) -> String? {
guard input.isEmpty == false else { return nil}
let result = input.filter({("A"..."Z").contains($0)})
return result.isEmpty ? nil : result
}
print(filterCapitalCharacters(fromString: ""))
// Question #4: convert [Any] to [Int]
let input : [Any] = [true, 1, "ravi", 2, "codecat15", false, "test"]
func convertToIntArray(inputValue: [Any]) -> [Int] {
guard inputValue.isEmpty == false else { return [] }
let result = inputValue.compactMap { item in
item as? Int
}
return result.isEmpty == false ? result : []
}
print(convertToIntArray(inputValue: input))