1. 程式人生 > >【Leetcode_總結】961. 重複 N 次的元素 - python

【Leetcode_總結】961. 重複 N 次的元素 - python

Q:

在大小為 2N 的陣列 A 中有 N+1 個不同的元素,其中有一個元素重複了 N 次。

返回重複了 N 次的那個元素。

 

示例 1:

輸入:[1,2,3,3]
輸出:3

示例 2:

輸入:[2,1,2,5,3,2]
輸出:2

示例 3:

輸入:[5,1,5,2,5,3,5,4]
輸出:5

連結:https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array/

思路:這個題目很簡單 詞頻大於1就好了

程式碼:

class Solution:
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        
        dic = {}
        for a in A:
            if a in dic:
                return a
            else:
                dic[a] = 1