C++ solution. simply one line.


  • 38
    K
    class Solution {
      public:
        bool containsDuplicate(vector<int>& nums) {
            return set<int>(nums.begin(), nums.end()).size() < nums.size();
        }
    };

  • 1
    Y

    cool looking, Python style solution, but source vector should be processed completely to get result. It could be impractical.


  • 0

    source vector should be processed completely to get result

    What do you mean? It is processed completely.

    It could be impractical.

    How so?


  • 0
    Y

    if you process source vector and fill set one by one, you can have result much earlier. For example, huge array with duplicate at second position.


  • 0

    Ah, it's a language problem, you meant to complain that the "vector is processed completely".

    Worst case is still the same, of course, a huge vector and no duplicates.


  • 0
    J

    Hi,
    Could you explain little bit how it works? It seems to me that the code is comparing the size of nums, did not try to find if there are duplicated numbers in the array.


  • 0
    K

    Hi, if there is duplication, the set will remove them and result in a unique list, which the size should be smaller than the original vector. Does that make sense to you?


  • 0
    J

    Integers in set<int> are all instinct.So if nums.size() > set<int>.size(),it means at least one integer is duplicate.


  • 0
    T

    Marvelous! You are such a genius!


Log in to reply
 

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