Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contains-duplicate/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
// time O(n) / space O(n)
func containsDuplicate(_ nums: [Int]) -> Bool {
var numSet = Set<Int>()
for num in nums {
if numSet.contains(num) {
return true
}
numSet.insert(num)
}

return false
Comment on lines +3 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift에서 Set을 사용하였군요

저는 조금 다른 방식으로 풀었는데
Set같은 자료구조가 아닌 주어진 리스트를 정렬 후,
1번 인덱스 부터 현재 인덱스랑 이전 인덱스 비교하며 중복이면 True를 리턴하도록 작성했습니다.

하지만 정렬 자체가 시간 부분에서 O(n*logn)이라 추천하지는 않습니다

코드 리뷰가 처음이라 메일이 계속 왔을텐데.. 다음부터는 제대로 리뷰하겠습니다..

}
}