[LeetCode]#645. Set Mismatch

Environment: Python 3.7

Key technique: dictionary, .values(), .index()

Example 1:

Input: nums = [1,2,2,4]
Output: [2,3]

Analysis:

For example 1, we use dictionary to generate each number and number frequency as [1:1, 2:2, 3:0,4:1]. Find frequency 2 and 0 and return them.

Solution:

class Solution:
def findErrorNums(self, nums):
dic={}
for i in range(1,len(nums)+1):
dic[i]=0
for n in nums:
dic[n]+=1
d1=list(dic.values()).index(2)
f1=d1+1
d2=list(dic.values()).index(0)
f2=d2+1
return[f1,f2]

Submitted result:

Lesson learn:

I never use this code before and it can return index(2) location in dictionary.

Reference:

https://www.twblogs.net/a/5c890310bd9eee35fc1487f1

Interesting in any computer science.