Can use single for loop that goes till length-1 with indexof() and lastindexof(). Check if both are different, then add to the set. This will work since we do not need count of occurrences. Set duplicates = new LinkedHashSet(); String input = "code decode"; for (int i = 0; i < input.length() - 1; i++) { if (input.indexOf(input.charAt(i)) != input.lastIndexOf(input.charAt(i))) { duplicates.add(input.charAt(i)); } }
I am really a big fan of CodeDecode and I want to say this video explanation is "THE BEST" we can find in the internet for FREE. You guys are really awesome and especially ma'am, the way you explain the logics are stupendous. This channel has helped me to grap multiple offers from various MNC's for more than 10 LPA package and I wanted to thank the entire team for your efforts on all your videos and playlist. Thanks again...!!!
I really like how you explain the concepts. Can you please make a video series (2-3 vids) for spring data jpa. A lot of interviewers have focused on this and sadly it was hard to fool them without complete knowledge on it. Thanks
Good Explanation. Please try to make videos on springs containers, JPA, Hibernate and other topics in spring. in most of the interview they asked about springs question with java.
www.ascii-code.com/ Look at this table. In java asciiis 256 bytes n not of 128 as in c etc. We map each string character to ascii value n store in integer array. Since it's easy to convert from character to ascii integer n vice versa, we use this technique to solve our problem.
I really appreciate your videos and lectures these helped me to prepare well. For the deplicate elements in string i have other solution; i.e public static Set duplicatesByUsingLastIndexOfMethod(String str) { Set duplicateCharacters = new LinkedHashSet(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (str.lastIndexOf(c) > i) { duplicateCharacters.add(c); } } return duplicateCharacters; } is it correct @CodeDecode ?
In the 3rd approach maybe if you use a LinkedHashMap instead of a HashMap then you can add the chars in the order they appeared in the string. In the other hand, I didn't get why O(log(n)). Great video thanks a lot!
String s ="codedecode"; //count of duplicate characters List list1=Arrays.asList(s.split("")); Map m =list1.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); System.out.println(m.entrySet()); //print only duplicate List list = Arrays.asList(s.split("")); Set set = new HashSet(); list.stream().filter(x->!set.add(x)).collect(Collectors.toSet()).forEach(System.out::println);
private static Set getDuplicateCharacters1(String s) { Set set = new LinkedHashSet(); // or HashSet if u dont mind of insertion order Map map = new HashMap(); for(char ch : s.toCharArray()){ if(map.containsKey(ch)) map.put(ch, map.get(ch)+1) ; else map.put(ch, 1) ; } for(Map.Entry entry : map.entrySet() ) if(entry.getValue()>1) set.add(entry.getKey()); return set; }
Very usefull...I had interview today and the interviewer ask the same question 😂😂😂
Hehe yess, interviewers have some set of common questions to ask from 🙂🙂
Wow
At that interview time you were fresher or experienced???
@@rushikeshgodase8498 i'm having 3+ year experience when I attended the interview
Can use single for loop that goes till length-1 with indexof() and lastindexof(). Check if both are different, then add to the set. This will work since we do not need count of occurrences.
Set duplicates = new LinkedHashSet();
String input = "code decode";
for (int i = 0; i < input.length() - 1; i++) {
if (input.indexOf(input.charAt(i)) != input.lastIndexOf(input.charAt(i))) {
duplicates.add(input.charAt(i));
}
}
I am really a big fan of CodeDecode and I want to say this video explanation is "THE BEST" we can find in the internet for FREE.
You guys are really awesome and especially ma'am, the way you explain the logics are stupendous. This channel has helped me to grap multiple offers from various MNC's for more than 10 LPA package and I wanted to thank the entire team for your efforts on all your videos and playlist. Thanks again...!!!
sure karthikeyan we have noted the request and we will try to create it in near future
Can also be solved using set - add chars to the set, if add() in set returns false, it means it is a duplicate value. Just store it.
I really like how you explain the concepts. Can you please make a video series (2-3 vids) for spring data jpa. A lot of interviewers have focused on this and sadly it was hard to fool them without complete knowledge on it. Thanks
Sure Nishank we will cover them too 👍🙂
*At **2:35** use getOrDefault method of map & keep on incrementing them with+1 by initial setting value 0 instead of if…else*
Btw nice video👌
👍👍
Thanks. That is simple and in detail.
Thanks Sourabh 🙂👍
Good Explanation. Please try to make videos on springs containers, JPA, Hibernate and other topics in spring. in most of the interview they asked about springs question with java.
Sure Akash. We do have many on our channel already 👍🙂
Excellent explanation, thank you
you're welcome suresh
Really good explanation 😃
Thanks
Very well explained
Thanks
It's really very helpful
Thanks varun
@@CodeDecode keep rocking "code decode" team..🤗🤗
int [ ] arrayforchar = new int [256] ;
why we use 256bytes size here?
www.ascii-code.com/
Look at this table. In java asciiis 256 bytes n not of 128 as in c etc. We map each string character to ascii value n store in integer array. Since it's easy to convert from character to ascii integer n vice versa, we use this technique to solve our problem.
Really nice video...
Thanks Swapnil🙂👍
Why we need to iterate again in 3rd approach? We can add duplicate elements into duplicates set directly after line no 26 right?
It’s great video
Thanks
I think we don't require second for loop in last 2 case's... In first for only we can add duplicate elements.
Isn’t the time complexity of map operations (put and get) is O(1) ? And, the 3rd approach’s overall time complexity is O(n) ??
Now map internally implements tree for handling collisions. So what is the time complexity of tree? It's o(logn). Hence o(nlogn)
I really appreciate your videos and lectures these helped me to prepare well. For the deplicate elements in string i have other solution; i.e public static Set duplicatesByUsingLastIndexOfMethod(String str) {
Set duplicateCharacters = new LinkedHashSet();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (str.lastIndexOf(c) > i) {
duplicateCharacters.add(c);
}
}
return duplicateCharacters;
}
is it correct @CodeDecode ?
Just add all char in set and then compare size of set with length of string. This will be more compact solution
Pls make one video on.. how to call stored procedure in hibernate or spring boot
Sure 👍👍
Please make a series on multithreading
Sure Raja 👍🙂
Great thanks
You are welcome
@@CodeDecode , sessions with you are worth revising. Thanks again.
In the 3rd approach maybe if you use a LinkedHashMap instead of a HashMap then you can add the chars in the order they appeared in the string.
In the other hand, I didn't get why O(log(n)). Great video thanks a lot!
Please do videos on Java Data Structers
sure krishna we will create it soon
Hi how your coding like this , can u just refer me the links to pratice such a analyst and speed in coding plz
Please create videos for Spring Transaction and Hibernate Transaction.
sure we will create video on it soon
@@CodeDecode thanks 😊
Instead of that you can add directly into hash set because they don't allow duplicate elements
How will you get the counts then? Also linkedhashset is a implemented class of set interface
Mostly asked questions in interviews
Yes it is frequently asked. 🙂
But now a days interviewer asked us to solve this through stream
th-cam.com/play/PLyHJZXNdCXsfcMboYwGoL6wKAFPxljz5W.html
String s ="codedecode";
//count of duplicate characters
List list1=Arrays.asList(s.split(""));
Map m =list1.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(m.entrySet());
//print only duplicate
List list = Arrays.asList(s.split(""));
Set set = new HashSet();
list.stream().filter(x->!set.add(x)).collect(Collectors.toSet()).forEach(System.out::println);
Solution using Java 8 Streams:
Set characterDuplicates = Arrays.asList(string.split("")).stream()
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet().stream().filter( e -> e.getValue()>1).map(e -> e.getKey()).collect(Collectors.toSet());
Still didn't get why you have taken array size of 256 though we have only 26 characters in second approach
private static Set getDuplicateCharacters1(String s) {
Set set = new LinkedHashSet(); // or HashSet if u dont mind of insertion order
Map map = new HashMap();
for(char ch : s.toCharArray()){
if(map.containsKey(ch))
map.put(ch, map.get(ch)+1) ;
else
map.put(ch, 1) ;
}
for(Map.Entry entry : map.entrySet() )
if(entry.getValue()>1)
set.add(entry.getKey());
return set;
}
Great explanation😊
Thanks Sangeetha 🙂