Skip to content

Conversation

sukrut57
Copy link

@sukrut57 sukrut57 commented Apr 4, 2022

  • Added java solution for leetcode 217 contains duplicate
  • -Added java solution for leetcode 242 valid anagram

public boolean containsDuplicate(int[] nums) {
HashSet<Integer> hashSet=new HashSet();
for(int i=0;i<nums.length;i++){
if(hashSet.contains(nums[i])) return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insted of doing this you can use hasSet.add(nus[i]).
code will look like->

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

@neetcode-gh
Copy link
Owner

Thanks but someone did the Java solution for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants