Simple java solution


  • 0
    Q

    Set only allows a given item to be added once.

        public boolean containsDuplicate(int[] nums) {
            Set<Integer> set = new HashSet<Integer>();
            for(int n : nums) {
                set.add(n);
            }
            if(set.size() < nums.length) return true;
            return false;
        }
    }

  • 1

    Replacing the last two lines of your code with

    return set.size() != nums.length;
    

    could have made your code even simpler :)


Log in to reply
 

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