The document is a tutorial by Mansi from Java Techies discussing how to find duplicate elements in an array using three different programming approaches: using a set, a map, and nested loops. Each method is explained with code examples, highlighting how to identify duplicates based on their occurrence frequency. The tutorial concludes with a demonstration of the outputs from each approach, encouraging viewers to engage with the content and ask questions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views
FindDuplicateElementsJava
The document is a tutorial by Mansi from Java Techies discussing how to find duplicate elements in an array using three different programming approaches: using a set, a map, and nested loops. Each method is explained with code examples, highlighting how to identify duplicates based on their occurrence frequency. The tutorial concludes with a demonstration of the outputs from each approach, encouraging viewers to engage with the content and ask questions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
Audio file
downloaded_audio_JAVA Program to find duplicate elements in an array [Most Asked
interview questions].mp3 Transcript 00:00:01 Speaker 1 Hello everyone so this is Mansi from Java techies. I am here to share with you one of the most commonly asked question. In most of the interviews that is related to programming so we I usually encountered this question again and again in most of the interviews to find all the duplicate. 00:00:20 Speaker 1 Elements in a given array. 00:00:22 Speaker 1 So there are multiple approaches to solve this particular question I have come up with three approaches I have already written the code for all of them, I'll just walk you through the code for all the three approaches and then maybe based on your requirement you can decide which one to go. 00:00:37 Speaker 1 For. 00:00:38 Speaker 1 So the first approach as I've listed here is you will be finding the duplicate elements. 00:00:43 Speaker 1 Changing the set. 00:00:45 Speaker 1 Another one is we will be using map to check the frequency. So we will have have a map into key value pair where into the key we will be storing the number and the value we will be storing the frequency or count of the number of times that particular number has occurred and we will just be printing those keys whose value will be more than one. 00:01:07 Speaker 1 Means to say whatever keys whatever numbers actually appeared more than once, we will be printing those. 00:01:16 Speaker 1 Again, the third one is we will be doing it. 00:01:20 Speaker 1 Two loops, that is, I and J2. For loops we will be using and iterating over the entire this integer array and then checking if I and G are equal, we will be printing that particular number. So I will just go through them one by one and explain you. 00:01:40 Speaker 1 Yeah. So the first one is using the set. So here what I have done is I have taken a hash set. I have created a hash set. Initially I have passed my integer array as part of it and I am just looping through this entire integer array using the for loop here. And what I'm doing is in the set. 00:02:01 Speaker 1 I am adding this particular number whatever number is there again and again. So what it does is this add method. I'm utilizing this add method to find the duplicate elements whenever we do like set dot add. 00:02:15 Speaker 1 This ad returns true or false. The return type of ad is basically true or false. So if particular, suppose if a number is already there in a set and you try to add it again, it will return false because as we know the set actually. 00:02:32 Speaker 1 Set actually contains unique numbers, right? So if we add a duplicate, it returns false. If we add a number for the first time, it will return true. So here I have checked if my set dot add is returning me false that means it is a duplicate. So I'm just printing out that particular number. 00:02:53 Speaker 1 Now moving on to the next approach, we are finding the duplicates using the map. Again, I have passed my entire integer array to it. So what I'm doing over here is I'm creating an integer map. Map is having key as integer and value as integer. So this key will be the number and this value will actually be the count of that number the number of times. 00:03:16 Speaker 1 That number has occurred in the array basically, so I have again iterated through the entire loop. 00:03:23 Speaker 1 What I am doing is I am checking if this map already contains that. 00:03:27 Speaker 1 Number I'm simply doing a put of number and I'm getting the previous value of count of that number and updating incrementing it by one else if that number is not there in the map then I'm doing a put off number and with initial count as one. So suppose for the first time a number occurs. 00:03:49 Speaker 1 It will actually go to the else block always and if again that number occurs, this contains key will actually return true and it will go to the if. 00:03:59 Speaker 1 Look, so suppose I have like this my integer arrays like this. 00:04:04 Speaker 1 So if suppose for the first time I got one as the number, So what what will happen? This contains key will actually return first this because initially there is nothing in the map so it will go to the else block. It will put number comma one that means one comma 11 number is having one as the initial count. 00:04:24 Speaker 1 Again, now it will go to two. Again. Contains key will return false because map doesn't contain the number two, it will go to the South block and it will put two comma one that is #2. 00:04:38 Speaker 1 Now again it will go. 00:04:41 Speaker 1 The next element that is 3 again 3 is not there and teams key will return first. It will go to the else block and it will put three comma one. Now it will come to two. Now this time map already. 00:04:54 Speaker 1 The element Key AS2 initially with the count one, that means. 00:05:00 Speaker 1 Contains key. This time will return true and what will happen is it will. 00:05:05 Speaker 1 To put of two comma, it is going to get the initial value. Earlier on it was 2 comma one. So this is going to give me one and I'm incrementing it by one that is. That means it is going to put two comma two. This means that the number two has a count 2. Likewise this this it will actually run. 00:05:26 Speaker 1 And it will update the count of all the particular keys into the. 00:05:31 Speaker 1 Now, at the time of printing, what I've done is I have been iterating through the map and I'm checking if the value of some key is greater than one, then only I'm printing it. That means it is a duplicate. Then only I'm actually printing this particular key. 00:05:50 Speaker 1 Now moving on to the next approach that is using the loop. So here you can see in both these approaches I have actually in my logic just used one single loop. That means it is of complexity O of north here also it is. 00:06:03 Speaker 1 See. 00:06:05 Speaker 1 Right. And here also, this puta just has one complexity which is negligible. So it is actually on complexity here the complexity. 00:06:15 Speaker 1 But at times, interviewers say that I don't want any inbuilt functions or things like that to be used. So in those cases you have to go for approaches like this. 00:06:26 Speaker 1 So although it has a higher complexity, but here we are not using any kind of inbuilt methods. So here what I'm doing is I'm iterating into two loops. I starting from zero and J loop is actually starting from a location after just adjacent to on the right side to I. 00:06:46 Speaker 1 So initially if I if I try to, you know dry run this for you. So initially a of I will be this. 00:06:54 Speaker 1 One and a of J will start from I + 1. That is 2, so you can see I am matching a of IE to a of J, so this will return first because a of IE will be one and a of J will be two so this will be false again I will J will be incremented. 00:07:10 Speaker 1 To. 00:07:11 Speaker 1 One so a of I is one and a of. 00:07:14 Speaker 1 J will be one now this condition will become true and it will print one which is duplicate. 00:07:22 Speaker 1 Now again, I will be actually incremented to one right? So. 00:07:28 Speaker 1 And J will start from here. That is. Sorry, a of I will be two a of I will be two that is a of one will be two and a of J will be 1 so this these are not equal so nothing will happen again. J will be incremented and now AJ will be two and a of five will be two so. 00:07:49 Speaker 1 These will actually match and this condition will be true, true, so it will print 2. 00:07:54 Speaker 1 So like this, this is actually working. So now I have just this you can see I've passed this particular array so you can see right now that 2. 00:08:05 Speaker 1 Three and eight are actually duplicate into this particular integer array. I'll just run it to with, with with all the three approaches, and let's see the output. 00:08:15 Speaker 1 So here you can see using the set we are getting 283 using map we are getting 238 using the loops we are getting 238 order is a little different over here because we are using hash set which doesn't maintain the order because of that. But it is printing out the duplicate elements. 00:08:34 Speaker 1 So that's it from my end. Thank you so much. Thanks for watching the video. Please do like, share, subscribe and if you have some questions, please do mention in the comments section. I'll be happy to answer all your questions. Thank you. Thank you so much. Bye bye.
The Effect of Accounting Information System (AIS) Practice in The Collection of Value Added Tax (VAT) : Study On Wolaita Sodo and Tercha Town Tax Revenue Authorities in SNNPR, Ethiopia
PDF (Ebook) Location-Based Services Handbook: Applications, Technologies, and Security by Syed A. Ahson, Mohammad Ilyas ISBN 9781420071962, 1420071963 download