Haven't seen anyone try this yet


  • 4
    G
    public class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> result = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++){
            result.add(nums[i]);
            if (result.size() != i+1){
                return true;
            }
        }
        return false;
    }

  • 0
    A

    how long does this take to finish all testcase?


  • 0
    R

    Interesting thought!! I can just came out with the hashmap way and it cost more time than your method.


  • 0
    G

    Lol I tried this the first time


  • 0

    same solution with little rewording:

      bool containsDuplicate(vector<int>& nums) {
           set<int> st(nums.begin(),nums.end());
           return (st.size()<nums.size());
        }

Log in to reply
 

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