forked from wty21cn/leetcode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq217-contains-duplicate.swift
45 lines (37 loc) · 1.25 KB
/
q217-contains-duplicate.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
//
// q217-contains-duplicate.swift
// leetcode-swift
// Source* : https://leetcode.com/problems/contains-duplicate/
// Category* : Hash
//
// Created by Tianyu Wang on 16/6/28.
// Github : http://github.com/wty21cn
// Website : http://wty.im
// Linkedin : https://www.linkedin.com/in/wty21cn
// Mail : mailto:[email protected]
/**********************************************************************************
*
* Given an array of integers, find if the array contains any duplicates.
* Your function should return true if any value appears at least twice in the array,
* and it should return false if every element is distinct.
*
**********************************************************************************/
import Foundation
struct q217 {
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var distinctSet = Set<Int>()
for num in nums {
if distinctSet.contains(num) {
return true
} else {
distinctSet.insert(num)
}
}
return false
}
}
static func getSolution() -> Void {
print(Solution().containsDuplicate([1,2,3,4,1]))
}
}