442. 数组中重复的数据

exiaohu 于 2022-05-08 发布

题目链接:442. 数组中重复的数据

使用集合,遍历数组。

遍历完毕后,集合中的元素就是只出现一次的元素。

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        ret = set()
        for i in nums:
            if i in ret:
                ret.remove(i)
            else:
                ret.add(i)
        return list(set(nums).difference(ret))