Another Method that we can sort the array and check its neighbor values. Arrays.sort(nums); for(int i=1;i
@bhavitvora93643 ай бұрын
Due to sorting, the time complexity will be O(nlogn). And using a HashSet will do it in O(n)
@vamshisundupalle6141 Жыл бұрын
Your explanation is so good
@satyamgupta6030 Жыл бұрын
thank you so much bhaiya for such a great explaination.
@nikoo28 Жыл бұрын
Keep watching
@pranaym1432 жыл бұрын
Simple and lucid explaination
@pentainamerica9 ай бұрын
isn't the time complexity n logn because it takes n to traverse through the array and for each element it might take log n to insert in hash set? or am I confusing with hash map?
@nikoo289 ай бұрын
Hashset insert takes place in O(1)
@lakshaysaini20708 ай бұрын
i have a doubt. the contains method of hashset also has a time complexity of O(n) and then there is for loop as well so shouldnt the time complexity be O(n^2). Is it the same as brute force approach?
@nikoo288 ай бұрын
Contains method works in O(1)
@lakshaysaini20708 ай бұрын
@nikoo28 thanks for replying back. But how is it O(1) cuz it will search through the whole set right?
@karthik-varma-15799 ай бұрын
GREAT
@jintodo6550 Жыл бұрын
very nice
@abhinavd22 жыл бұрын
Super ❤️
@kadirv9 ай бұрын
Sorry, as for the time-complexity - you check for the element in hash-set. How exactly does this happen? Won't all the has set elements be checked against to check if a number is in there or not? then the time complexity will be n square?
@nikoo289 ай бұрын
Checking in HashSet happens in O(1) time
@anshumansolanki6775 Жыл бұрын
Why are we using a HashSet and not a HashMap Nikhil ??
@nikoo28 Жыл бұрын
what advantage would a hashmap give you?
@anshumansolanki6775 Жыл бұрын
@@nikoo28 you are right we cant say it will improve the algorithm, can we say its one of the solutions? If I find the entry in map I can say there is a duplicate
@SatishSingh-eb5tq9 ай бұрын
We could have avoided the contains check. add() will return false if the number is already present!!
@rajabhowmick134210 ай бұрын
for (int n : nums) { if(!set.add(n)) { return true; } }