Program to Find Duplicate character in a String Coding Interview Question and Answer | Code Decode

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ม.ค. 2025

ความคิดเห็น • 65

  • @vinothkumarpalanivel4355
    @vinothkumarpalanivel4355 2 ปีที่แล้ว +19

    Very usefull...I had interview today and the interviewer ask the same question 😂😂😂

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +2

      Hehe yess, interviewers have some set of common questions to ask from 🙂🙂

    • @rishiraj2548
      @rishiraj2548 2 ปีที่แล้ว

      Wow

    • @rushikeshgodase8498
      @rushikeshgodase8498 2 ปีที่แล้ว

      At that interview time you were fresher or experienced???

    • @vinothkumarpalanivel4355
      @vinothkumarpalanivel4355 2 ปีที่แล้ว

      @@rushikeshgodase8498 i'm having 3+ year experience when I attended the interview

  • @umangshah9305
    @umangshah9305 2 ปีที่แล้ว +4

    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));
    }
    }

  • @karthigeyanm3376
    @karthigeyanm3376 2 ปีที่แล้ว +2

    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...!!!

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      sure karthikeyan we have noted the request and we will try to create it in near future

  • @pratiyushanand6454
    @pratiyushanand6454 2 ปีที่แล้ว +2

    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.

  • @nishankjain8739
    @nishankjain8739 2 ปีที่แล้ว +3

    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

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      Sure Nishank we will cover them too 👍🙂

  • @_satveer
    @_satveer ปีที่แล้ว +1

    *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👌

  • @kalekar100
    @kalekar100 2 ปีที่แล้ว +2

    Thanks. That is simple and in detail.

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +1

      Thanks Sourabh 🙂👍

  • @mysteriouscartoontales
    @mysteriouscartoontales 2 ปีที่แล้ว +3

    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.

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +1

      Sure Akash. We do have many on our channel already 👍🙂

  • @sureshmanne7245
    @sureshmanne7245 2 ปีที่แล้ว +2

    Excellent explanation, thank you

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      you're welcome suresh

  • @sonaligayake9240
    @sonaligayake9240 ปีที่แล้ว +1

    Really good explanation 😃

  • @ashwinmarathe9844
    @ashwinmarathe9844 ปีที่แล้ว +1

    Very well explained

  • @varunsiddarth1265
    @varunsiddarth1265 2 ปีที่แล้ว +1

    It's really very helpful

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      Thanks varun

    • @varunsiddarth1265
      @varunsiddarth1265 2 ปีที่แล้ว

      @@CodeDecode keep rocking "code decode" team..🤗🤗

  • @kancharlasrimannarayana7068
    @kancharlasrimannarayana7068 2 ปีที่แล้ว +3

    int [ ] arrayforchar = new int [256] ;
    why we use 256bytes size here?

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      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.

  • @swapnilmishra697
    @swapnilmishra697 2 ปีที่แล้ว +1

    Really nice video...

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      Thanks Swapnil🙂👍

  • @gyvsr
    @gyvsr 2 ปีที่แล้ว

    Why we need to iterate again in 3rd approach? We can add duplicate elements into duplicates set directly after line no 26 right?

  • @chiranjibidangal6184
    @chiranjibidangal6184 3 หลายเดือนก่อน +1

    It’s great video

    • @CodeDecode
      @CodeDecode  3 หลายเดือนก่อน

      Thanks

  • @ponnamramesh4569
    @ponnamramesh4569 2 ปีที่แล้ว +1

    I think we don't require second for loop in last 2 case's... In first for only we can add duplicate elements.

  • @arutsudar
    @arutsudar 2 ปีที่แล้ว +1

    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) ??

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +5

      Now map internally implements tree for handling collisions. So what is the time complexity of tree? It's o(logn). Hence o(nlogn)

  • @sourabhchougule5642
    @sourabhchougule5642 9 หลายเดือนก่อน

    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 ?

  • @jaivikkotadiya730
    @jaivikkotadiya730 10 หลายเดือนก่อน

    Just add all char in set and then compare size of set with length of string. This will be more compact solution

  • @akshaykohale3121
    @akshaykohale3121 2 ปีที่แล้ว +1

    Pls make one video on.. how to call stored procedure in hibernate or spring boot

  • @rajashekar-bu8xd
    @rajashekar-bu8xd 2 ปีที่แล้ว +1

    Please make a series on multithreading

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      Sure Raja 👍🙂

  • @rishiraj2548
    @rishiraj2548 2 ปีที่แล้ว +1

    Great thanks

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +1

      You are welcome

    • @rishiraj2548
      @rishiraj2548 3 หลายเดือนก่อน

      @@CodeDecode , sessions with you are worth revising. Thanks again.

  • @felix2916
    @felix2916 2 ปีที่แล้ว

    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!

  • @krishnareddy-ro2rp
    @krishnareddy-ro2rp 2 ปีที่แล้ว +1

    Please do videos on Java Data Structers

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      sure krishna we will create it soon

  • @Ravikumar-gj6qw
    @Ravikumar-gj6qw 2 ปีที่แล้ว

    Hi how your coding like this , can u just refer me the links to pratice such a analyst and speed in coding plz

  • @rocker31590
    @rocker31590 2 ปีที่แล้ว

    Please create videos for Spring Transaction and Hibernate Transaction.

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +1

      sure we will create video on it soon

    • @rocker31590
      @rocker31590 2 ปีที่แล้ว

      @@CodeDecode thanks 😊

  • @akhilchowdary814
    @akhilchowdary814 2 ปีที่แล้ว

    Instead of that you can add directly into hash set because they don't allow duplicate elements

    • @AbhishekPandey-mi1wi
      @AbhishekPandey-mi1wi 2 ปีที่แล้ว

      How will you get the counts then? Also linkedhashset is a implemented class of set interface

  • @tanveersyed1049
    @tanveersyed1049 2 ปีที่แล้ว +3

    Mostly asked questions in interviews

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว +1

      Yes it is frequently asked. 🙂

  • @ILuvBilli
    @ILuvBilli 2 ปีที่แล้ว +1

    But now a days interviewer asked us to solve this through stream

    • @CodeDecode
      @CodeDecode  2 ปีที่แล้ว

      th-cam.com/play/PLyHJZXNdCXsfcMboYwGoL6wKAFPxljz5W.html

  • @elangovan.s7820
    @elangovan.s7820 2 ปีที่แล้ว

    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);

  • @sravandatha9511
    @sravandatha9511 ปีที่แล้ว

    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());

  • @Im_gpd
    @Im_gpd 3 หลายเดือนก่อน

    Still didn't get why you have taken array size of 256 though we have only 26 characters in second approach

  • @samahmahdi5511
    @samahmahdi5511 9 หลายเดือนก่อน

    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;
    }

  • @sangeethaponnusamy579
    @sangeethaponnusamy579 ปีที่แล้ว +1

    Great explanation😊

    • @CodeDecode
      @CodeDecode  ปีที่แล้ว

      Thanks Sangeetha 🙂