题目要求
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
题目分析及思路
给出一个整数数组,判断这个数组是否包含重复值。若有任意值出现两次及以上则返回True,若每个值都只出现一次,则返回False。可以使用集合的特性获取数组中不同字符的个数,若和原数组的长度相等,则返回False。否则则返回True。
python代码
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
nums_s = set(nums)
if len(nums) == len(nums_s):
return False
else:
return True