Python Solution using sum of numbers

  • 0

    Set() is useful in this case to unique the list.

    class Solution(object):
        def findErrorNums(self, nums):
            :type nums: List[int]
            :rtype: List[int]
            n = len(nums)
            # sum of error list
            nums_sum = sum(nums)
            # sum of normal list
            all_sum = n*(n+1)/2
            # sum of unique elements
            unique_sum = sum(list(set(nums)))
            return [nums_sum-unique_sum, all_sum - unique_sum]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.