LeetCode Contains Duplicate Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

I am beginner and I started leetcode literally three days ago and even some easy problems a bit hard for me but the more practice I do I feel more that I am becoming better. Thank you for your content

WalkWithM
Автор

HashSet approach can be simplified further. Just call the "add" method on the set for each element in the array and if "add" returns false (meaning the set already has the element) return true.

shivashisharora
Автор

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

prakashkrishna
Автор

If the hashset method is supposed to be faster than the sorting method, then why is the the sorting method faster than the hashset method based on the runtime submissions?

vincentwang
Автор

It's TIMSORT, .sort() in Java. Thanks for valuable content

vikas
Автор

Its much easier in JS:
const unique = new Set(arr);
return unique.size !== arr.length;

Booyamakashi
Автор

How does that loop work when reaching the last number? Comparing to i + 1 is null right?

alxx
Автор

In python I just did this:
return len(set(nums)) != len(nums)
Is there a reason not to do it like this?

PeterKoenen
Автор

How about nested loop? starting from i = 0, j = i + 1 etc

giorgi
Автор

“Too easy”… me sitting here for half an hour trying to solve this stuff 😭

KaisarAnvar
Автор

shouldn't it be for (int i = 0; i < nums.length ; i++){... for the second solution. cause say arr length=4, it checks 0 thru 3, 3 being index of fourth element?
P.S I'm a beginner

coolwhip-ucqt
Автор

Why did hashset take longer (19ms) when it's O(n) compared to sorting O(nlogn) 7ms? Thanks for the simple solutions, I couldn't quite figure out how to do it without 2 for loops lol

edmund
Автор

Love the video, want to ask why do you add the element to the hash set if its not found

eboselumeenahoro
Автор

Use XOR with index, will be quicker and efficient

hamsalekhavenkatesh
Автор

What if we add binary bits and capture %2 for the sum of bits in each place value.. finally of we get 0 no duplicates

shivahere
Автор

Sorting method can itself check for duplicate : insertion sort, bubble sort etc

Ankushbindlish
Автор

why not put all nums in a Set and then compare the size of the Set to the size of the nums array. If they're the same, no dupes, else dupes.

ericsodt
Автор

Thanks for the video! Would really appreciate if you could increase the volume :)

Shreyas-smjt
Автор

Love your work! How do you record your content? Both camera and screen?

theinvestorengineer
Автор

The built in sort in Java is quicksort .

SK-ybbx
join shbcf.ru