current position:Home>[leetcode] 38. There are duplicate elements - Java version

[leetcode] 38. There are duplicate elements - Java version

2021-08-26 16:37:32 One coding

Welcome to subscribe to 《leetcode》 special column , A daily topic , Daily progress

java Set Self weight removal , If the length after weight removal is less than the original length , Then return to true

——leetcode Hot comment on this question


hello , Hello everyone , I am a .

Confused algorithm , A rare confused


217. There are duplicate elements

difficulty : Simple

Given an array of integers , Determine whether there are duplicate elements .

If a value exists, it appears at least twice in the array , The function returns true . If every element in the array is different , Then return to false .

Example 1:

 Input : [1,2,3,1]
 Output : true

Example 2:

 Input : [1,2,3,4]
 Output : false

Example 3:

 Input : [1,1,1,3,3,4,3,2,4,2]
 Output : true



  • utilize hashset The principle of weight removal
  • If you return... When adding an element false, There is


all leetcode Code synchronized to github

welcome star

/** * @author yitiaoIT */
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x : nums) {
            if (!set.add(x)) {
                return true;
        return false;


Complexity analysis

  • Time complexity :O(N)


Treasure hunt

Today is the first day to insist on brushing questions 38/100 God

Everyone give the thumbs-up 、 Focus on 、 Collection 、 Comment on 、 subscribe Is the biggest driving force of creation

More algorithm problems are welcome to pay attention to the column 《leetcode》

In order to give back to all fans , In town , I've prepared some for you Algorithm teaching videos and e-books

If you need a partner, you can send me a private letter to get , But be sure to pay attention first ! Otherwise, you can't send private messages !

copyright notice
author[One coding],Please bring the original link to reprint, thank you.

Random recommended