Review Note

Last Update: 07/03/2024 02:06 AM

Current Deck: LeetCode

Published

Currently Published Content


Front
217. Contains Duplicate
  • Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Back
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        seen = set()
        for n in nums:
            if n in seen:
                return True
            seen.add(n)
        return False

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> s;
        for (int n : nums) {
            if (s.count(n))
                return true;
            s.insert(n);
        }
        return false;
    }
};

Current Tags:

Arrays_&_Hashing

Pending Suggestions


No pending suggestions for this note.