Java coding interview questions || Print duplicate occurrences of string in java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] anyway welcome back so in this video we're going to understand a very popular it will be posted now it is I think the duplicate occurrence of spring so it's a very popular coding interview question nowadays I'll give you to solve it and we're going to start back up in here by using each other all right so first of all they'll give you a screen like this let's say string STR equal to R M M learning Java Java right let's say this is your string right so you can see there are few words over here there are very multiple tens right mmm tell Java Java this these are occurring multiple times right so if you talk about over here I this is occurring one time and a is occurring two times and learning is occurring one time and this Java is happening to dance right so over here you can see this game and this Java is occurring two times right mmm Java Java so you need to only display a man Java because they are offering multiple time you don't need to display this all right so first of all how to do that okay how we can solve this particular program so first of all I'll suggest you to stop this video and for think for a bit okay and just just this tiny thing okay how can you solve this [Music] let's go ahead okay all right here one thing I'm going to do first first I need to separate all this whatever here okay I am this same learning chopper job and you separate all these things okay so did you separate all this thing I'm going to use a split method in string API so over STR I'm going to use this method split and how much bleep I'm going to split it by using the space so let's say I do this and here I just give in a space so nobody's come to split it where there is a space right sorry there is a space please gonna split it over here okay and this is going to return me a stream all right yes now this s is containing all this one right so I'm just come back pistols I'm in in learning right so I got all my words over here right okay great alright so now I want to insert this particular things to attempt okay so I want to I want to maintain a table of reviews which keep varying the key amirite a is the string suppose this I put high over here and I'm gonna maintain how many times they are occurred okay so for an example of linear and I before I insert this I will prepare to this table and watch it either there is AI already predict temperatures right now in this table this is empty there is no I predict a preview so I'm gonna insert a over here and obtain the value is what because there is no longer present previously so I is occurring only one time so now I'm going to the next and next and end up in here this is n so n is already there no it's not there then install it and give it a very one all right now the next thing is again so I it's already there yes it is there okay if it is there then update the value by plus 1 so 1 plus 1 now the value will be 2 okay say again the next one over here is learning so this so before entering learning again I'm not sure if the learning is credit of munich the table no so please enter it and let's give it a penny Wow alright now again Java Java is already there no it's not there and enter it and give it a value 1 now the next element is again Java which I smell it over here so just update this one too - all right so this is how I maintain a table over here and from here I'm gonna fetch how many times on this particular water seller credit all right but how I'm going to solve it it is so great I'm gonna use a hash Matthew right think about your map your map is also maintaining a table right using key value pair let's say this is the map object right and using this map table I can solve this particular program by using the put method and get a method for putting and I'm going to use put method and to get the very I'm gonna use the get method right so how do you solve that now let's go to the computer and let's see that right alright guys so now let's say it practically right so I do have a class over here called duplicate string and I have a main method right so I should have a method over here let's say find duplicate word ok let's say I should have a method like this and I should pass a string over here let's say I am am learning Java Java and now this particular method should say me that dude this aim is appearing over here two times this Java is appearing over here two times all right it should give some messages to me like that right so how to create a method like this so let's create a method over here let's say find duplicate word and this particular method should take a string as argument so let's say string SDR I should keep your body open and close let's say this particular method returns nothing I should make it void and let's make a static so that I can access it without creating an object right now the error is gone okay so now there is no implementation with this right so let's start scoring with it so first of all whatever string I'm getting over here I mean this is the one that I'm getting with this STR so now let's split this STR so I'll say STR dot split and I'm gonna split this by using a space right and I'll enter a semicolon and let's store it with a string array so string is a string s equal to STR just split okay so now it's going to split everything where there is a space okay so to view this I'm gonna use a for loop so that I can loop over each of the carriage of the word that present over here in the string with the string array so I'll say here string and tamed string and I'll put this variable which contains all of my string array right I mean all my string right so I'll put s over here and if I'll do is this out over here if I'll print everything then I should get all of my string over here so if I will run it okay so let me do a right click run as java application proceed and there you go so I'm getting all my string over here I mmm learning Java in Java right okay so I don't want to print this thing just for debugging I just printed it so what I want over here is actually I want to insert all the string that I'm getting over here to a map okay do a table I want to maintain a table so for that I need to create a map right so let's create a map over here let's say hash map and my key should be a string as we have discussed and the values should be an integer solicitor integer here and hmm equal to a new hash map and there you go so I've created a map so what I want to do over here I'll do hmmm that put so it's time you loop over a string I mean each time you have a string you enter this string to the map right and update the value h1 or just give it a value as 1 ok so right now I have inserted you know whatever the string I'm getting over here I have inserted it to a map Colosseum right so if I'll just print if I will print the map variable over here then I should get all these values like this but see now we have a problem now all these map variables is just showing us one so Java is one learning is one M is also occurring one time so why it is doing this because for each of the duplicates so whenever a map is getting a duplicate key it is just again updating the value to 1 all right so that's why we are getting all the values over here as one so we didn't want it right so before we insert a value to our map we need to check okay we should check whether this particular string is already present inside the table or not ok so if it is present then there's a duplicate and if it is a duplicate we need to add one to our previous value that is hold ok so what I mean by that so I should have a if closed over here I should check if hm dot get if my map is containing a key already right and if this key is not equal to null see if the key is equal to null that means that's not a duplicate right that means is that key has not been inserted to our map right that means this particular clause should execute this particular line should execute but if the stem string is not equal to null that means it has some value and if it has some value let's update that value to +1 right so how can I do that so hmm God put and here let's keep the same key over here damn string and the value here we need to update it by 1 right I mean we should do plus 1 to our existing value so how can you do that we'll do sa m dot get get the value which is available right now with the temp string and then with this value add 1 over here okay so there you go and put this thing with the else clause so if I will do like this then that's ready right now I'm printing my map over here again so if I run this application right now you can see you're just showing is Java is two times learning is one time I is one time and M is two time right this order is not important because in a map you cannot predict order right but this is not what we want okay so what we want is we should only display those particulars word or those particular string which have a value more than one I mean which is a duplicate okay if it is a duplicitous value should be more than one right over here is Java is a duplicate and M is a duplicate right so how can we do that so for doing that what we need to do I don't need this line I just have written it just to show you okay so to view all the duplicates over here we need to have all the keys of our map and using this particular key we can check either our key value is 1 or greater than 1 if it is greater than 1 then that's a duplicate so for this I'm gonna use iterator ok so I'm going to say HM which is my map object dot key set which give me only the key and I'm gonna run a turret over it and I'm gonna store this with a iterator and let's give it a type called string because my key is of string type and let's say it damp string okay there you go so my interior is ready right now lest iterate over each of the ten string okay each of the key so I'm gonna have a while loop over here while just a minute let me type it again while and I should give it a body let's open and close and in the while loop I should check whether any other element is available in the iterator and if yes just give me that particular string okay so else I should say at M string dot next okay so this next should give me a string so let me stir it over here string let's attempt alright so once I get this particular string so this string is a key right I need to check whether the value which is associated with this particular key is 1 or greater than 1 if it is greater than 1 there's a duplicate I want to display it if it is not greater than 1 then that's not a duplicate so I don't want to display it so to do that what I'm going to do over here I'm going to make use of a if clause and if door key HM that gate of this key temp is greater than 1 if it's greater than 1 then only I want to display this right so I'll just have a simple print statement and I'll say the word and I should write temp because there's the word appear and I should say hmm dot gate of key temp number of times right number of times alright so right now let's run this application so you can see right now the word Java appear to number of times okay let's let me give a space over here and let me run it again and there we go so the word Java appeared to number of times and the word aim up here to number of times I do have some typos here it it B so I'm getting my duplicate in the output right so let me try something else let me say something different let me say I am am am a 1/4 okay so there you go so here M is appearing 3 times a is appearing there is 2 space here okay and a is appearing 2 times right and this corner is one time and I is one time so if I will run it you'll get the word a appeared the number of times the word em up your 3 number of times right so there you go all right guys so this is for how we can find the duplicate occurrence for a string and how we can display only the duplicate element right and what about if I want to find the duplicate occurrence of a character in a given string alright so we have to do the exact same thing so there's a tux for you you have to do it ok but let me give a little bit of clue to you right so let me copy this and paste it over here again just to save some time and let me say it not what I let's say find duplicate characters ok and you let's say if I'm going to give a duplicate character like Java j2ee now this particular method should tell me how many times this J occurred how many times is a occurred how many times is vu occur and this occurred as well right and it should only display the characters which are duplicates right so for this again I need to create a matter so let me do some part of it for you guys so let me open and close it and let me make this matter static void and this method should accept a string string STR and you have to do the exact same thing but only one thing is different you need to get each of the character over here instead of string right over here we used to do wish to get all the string we have to split right but over here we are not going to split we're going to get the index and using the index we are going to get all these characters over here J FD a and our listings right so let's see how we can do that so for this I'm gonna use a for loop over here so I'm gonna do it really really quick right so right in I equal to 0 I less than equal to STR dot length and I plus plus and over here I'm gonna do STR that carrot and I'm gonna use the index I so I is right now 0 a little H the index which is off 0 and it'll just give me a character so there we go and once you get the character you can do the same thing so right now what I can do over here I can create a map so I can write create a map like this hash map and I'll take character and for value I'm going to take integer and let's give it a reference SM it killed a new hash map alright so now let me enter this one HM that foot and you can see I can do is see and the value can update to one but before this we need to check right so I'm going to write a if clause here and inside the if clause you have to check it whether this tickler value that you're inserting is already available or not sorry you need to get it and there we go and you have to huge the see if the sea is present so if the gate of C is not equal to null here you need to do what HM that put off you have to put the character here and the value we have to update it HM that gate off C off and you have to update it by one right so there you go so it's ready so right now if I'm gonna print it are going to get all the duplicates element and all your elements which has been there in your map so you can see all right so before we run it I skip the else clause over here so let me put it over here and there we go right now if I'll do a right click and so you can see in your java j2ee say appears two times as one time and es two times and a is 2 times knowledge right so right now we can do the exact same thing that we have done it for for this particular matter it can write the exact same iterator over this method also so you can write the exact same iterator over here in this method you just played off only duplicate characters alright so that's it for today thank you very much for watching and still if you have any doubt put it in a comment I'll get back to you and assist you on that thank you very much guys for watching have a nice day [Music]
Info
Channel: Selenium Express
Views: 173,836
Rating: 4.8700104 out of 5
Keywords: string in java interview questions, print duplicate characters in a string java, Print duplicate occurrences of string in java, print repeated characters string java how to print duplicate characters from string in java, remove duplicate characters from string in java, string interview questions in java, collection interview questions for experienced in java, collection interview questions, java experience interview questions and answers, selenium express, hashmap interview questions
Id: 2PlWhcLi1JQ
Channel Id: undefined
Length: 21min 40sec (1300 seconds)
Published: Thu Mar 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.