본문 바로가기

알고리즘

[해쉬] N-Repeated Element in Size 2N Array - LeetCode Swift

 

문제 : https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

 

class Solution {
    func repeatedNTimes(_ nums: [Int]) -> Int {
        var dic = [Int:Int]()
        
        for num in nums {
            if dic[num] == nil {
                dic[num] = 0
            } else {
                return num
            }
        }
       
        return 0
    }
}

 

해쉬를 이용해서 똑같은 key가 나오면 그 key를 리턴하도록 했다.

반응형