Remove Duplicate Elements from Unsorted Array - Java Code

  Рет қаралды 100,709

Programming Tutorials

Programming Tutorials

5 жыл бұрын

Write a java program to remove duplicate elements from an unsorted array. In this tutorial, I have explained three approaches to solve this problem.
Reverse String - • Reverse a String in Ja...
Code Link: webrewrite.com/remove-duplica...
Binary Search Video Tutorial - • Binary Search | LeetCo...
Find second smallest number in an array without sorting - • Find Second Smallest N...
Remove duplicate from sorted linked list - • Remove Duplicates from...
Cracking the coding interview - amzn.to/3h70jXy
Data structure and algorithms made easy - amzn.to/3jgMl7U
LeetCode May Challenge PlayList - • First Bad Version | Fi...
LeetCode April Challenge PlayList - • Move Zeroes LeetCode |...
Paypal - www.paypal.me/programmingtuto...
Website - webrewrite.com/
Code Link: webrewrite.com/remove-duplica...

Пікірлер: 57
@sureshnaidu3519
@sureshnaidu3519 4 жыл бұрын
Very nice approach and explained very well. Subscribed. Thank you
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Thank you.
@chrisw9597
@chrisw9597 4 жыл бұрын
These videos are a godsend. I don't normally comment, but you deserve my appraisal. Thank you!
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Thank you Christopher.
@jeetamitshah6194
@jeetamitshah6194 2 жыл бұрын
In the first approach, what about the remaining 2 positions in the array? You left them empty. The original array is of length 7 and new array with only unique elements has a length of 5, so what about the remaining 2 positions in the array? Java doesn't allow dynamic arrays so the length of the array is not changing .
@RajeevKumar-gk1qo
@RajeevKumar-gk1qo 3 жыл бұрын
Explained very well. You are great.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Thanks a lot
@ranadheersannihith2362
@ranadheersannihith2362 4 жыл бұрын
In the first approach, every time the last element is getting added arr[j++]=arr[len-1], then if the last element is 5 and not 6 then 5 will again be added at the end and the output could be 1,2,4,5,5.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Hi Ranadheer, You missed that condition if (arr[i] != arr[i + 1]) { arr[j++] = arr[i]; } In this condition, Second last element is already compared with last element. So, in that case second last element won't be added. You can run the code and check the output.
@ranadheersannihith2362
@ranadheersannihith2362 4 жыл бұрын
@@ProgrammingTutorials1M I am not talking about second last. Consider 5 as the last element and there is no 6. Then last element 5 has to be added to our output and our output would be 1,2,4,5 and 5.
@bharathkoneru4008
@bharathkoneru4008 3 жыл бұрын
@@ranadheersannihith2362 no man, you got it wrong. arr = {1,2,4,4,5,5,5} 5 ! =5 is not satisfied till last element, so 5 will not be added to the array on iteration using for loop. {1,2,4} 5 only gets added when we add manually last element of array. arr = {1,2,4,5}
@Rahul39920
@Rahul39920 2 жыл бұрын
With this approach the problem is with line no: 74. This will throw ArrayIndexOutOfBoundsException when the size of the array is 0. And it adds extra burden to handle last element of Array. We can certainly improve this.
@shreyaroy4773
@shreyaroy4773 3 жыл бұрын
Awesome approach sir! Searched a lot then found this video! Can you please say how to implement the 2nd and 3rd approaches in C++?
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Hi Shreya, I know only java.
@shreyaroy4773
@shreyaroy4773 3 жыл бұрын
@@ProgrammingTutorials1M ok sir! Thank you!
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
Code link is present in the description box
@SaiTeja-ym2er
@SaiTeja-ym2er 2 жыл бұрын
Very Good explanation.Thanks
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
You are welcome
@maheshkumarb7729
@maheshkumarb7729 2 жыл бұрын
Hi All, I tried for this input -> int[] arr = {4,2,1,5,5,6,7,8,8,9}; Its removing the duplicate values but was not printing the last value i.e., 9 in this example. We need to use --> for (int i=0; i for (int i=0; i
@marcos.a8814
@marcos.a8814 Жыл бұрын
Thank you so much!! for hashing and set the -1 must be removed, only keep it for the first code, which is great because I don't really understand why it needs to be -1
@sakhi_dikshit
@sakhi_dikshit 3 жыл бұрын
Thank you so much😇😇 please upload more videos 🙏
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
I will try my best
@explorelife2418
@explorelife2418 4 жыл бұрын
The code is amazing. Thank you so much. Subscribed 😊😊 Can you please tell how to find the non repeating elements in the array? i.e the elements which occur only once.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Thanks for your comment. Here is the video link : kzbin.info/www/bejne/rKnCpKGha7eEopI
@7smessi803
@7smessi803 9 ай бұрын
Awesome approch brother'❤️❤️❤️
@ProgrammingTutorials1M
@ProgrammingTutorials1M 9 ай бұрын
Thanks ✌️
@V.LaxmanaVyaas
@V.LaxmanaVyaas 22 күн бұрын
Sir the output is in ordered one..not in unordered one sir
@swapniljadhav5461
@swapniljadhav5461 5 жыл бұрын
Good approaches. Is it possible to remove duplicates from unsorted array without using any collection. For example in O(1) or O(log n) space complexity
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
Hi Swapnil, O(1) and O(logn) might not be possible for unsorted array. You need to traverse complete array to figure out the duplicates.
@abdulrahmanabdullahi1875
@abdulrahmanabdullahi1875 2 жыл бұрын
Why is the sorting important when trying to remove the duplicates from an array?
@venkyvenky4261
@venkyvenky4261 Жыл бұрын
But output is not in order You changed the order sir
@mohdamirquadri5796
@mohdamirquadri5796 3 жыл бұрын
Sir instead of void in method use type and return value beacause in every interview there ask this pattern ..
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Yes, In my new videos i have implemented this approach. You can check it.
@UnprivilegedDelhi
@UnprivilegedDelhi 3 жыл бұрын
what if we need to maintain the order of the array, how can we implement it without sorting the array?
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
You can do it by using set data structure.
@sivaram9439
@sivaram9439 5 жыл бұрын
is first method is exactly crt for all the testcases....???
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
Yes
@debayanroy6325
@debayanroy6325 Жыл бұрын
If you use LinkedHashSet instead of HashSet you can even retain the order.
@sakhi_dikshit
@sakhi_dikshit 3 жыл бұрын
Please make video on hackerrank problems.
@shubhambhosale8467
@shubhambhosale8467 2 жыл бұрын
make an empty set and insert all array value in that set it get sorted+remove duplicate as well thanks me later
@asifbasheerkhan2855
@asifbasheerkhan2855 2 жыл бұрын
we should you linkedhashset instead of simple hashset because order in not preserved .
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
If you want to preserve order then you can
@ArunKumar-ko8po
@ArunKumar-ko8po 2 жыл бұрын
thank you :)
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
You're welcome!
@Baby_Boy_Mintu
@Baby_Boy_Mintu 2 жыл бұрын
Thanks sir
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
Welcome
@bryanlozano8905
@bryanlozano8905 Жыл бұрын
Can you use merge sort, and eject duplicates during merge routine? Or would all of those repeated shifts hurt the algorithm? I guess you can at least record the index of elements that are to be ejected during sorting.
@ProgrammingTutorials1M
@ProgrammingTutorials1M Жыл бұрын
But in this case the time complexity would be O(nlogn). There is a tradeoff between time and space here. Using extra space we can solve this problem in linear time.
@reefmansour3630
@reefmansour3630 3 жыл бұрын
Thaaaaaaannnnnk you the j++ is a smart move !!! thanks
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Please like and share with your friends.
@Tangoez
@Tangoez 3 жыл бұрын
Shabbaruoureyyyy shabana pureyyyy Heyyyyyyy shaburshaburaaa pureyyyyyyy
@alokkachhap4171
@alokkachhap4171 4 жыл бұрын
-> what it is
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
This is the lambda expression which is introduced in java 8.
@jhoanmateofrancovargas1405
@jhoanmateofrancovargas1405 4 жыл бұрын
gracias por el vídeo,fue fácil entenderte aunque no hablaras mi idioma xd
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
muchas gracias
Find Maximum Difference between Two Array Elements
10:32
Programming Tutorials
Рет қаралды 23 М.
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 100 МЛН
How to create a record management system #recordmanagementsystem
14:02
Vikrant Kumar(Coding channel)
Рет қаралды 125
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 495 М.
Remove Duplicates from sorted Array
21:22
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 165 М.