Find First Repeated and Non Repeated Character in a String Using Java8

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

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

  • @MohitRai-t9r
    @MohitRai-t9r 9 วันที่ผ่านมา

    Hi you are doing amazing job thank you for that, but this was complex solution we can try this approach if you want
    public static void main(String[] args) {
    String str = "Hello World";
    Set set = new HashSet();
    Set set2 = new HashSet();
    Character first = str.chars().mapToObj(o -> (char) o)
    .filter(c -> !set.add(c)).findFirst().get();
    System.out.println("First Repeating char "+first);
    Character firstNonRepeatingChar = str.chars().mapToObj(o -> (char) o)
    .filter(c -> set2.add(c)).findFirst().get();
    System.out.println("First nonRepeating char "+firstNonRepeatingChar);
    }

  • @PriyaSingh-w2f
    @PriyaSingh-w2f 10 หลายเดือนก่อน +3

    If u would have explained the code that will be very helpful, you are just writing the code

  • @vipintekade6400
    @vipintekade6400 ปีที่แล้ว +7

    Confusing code 😞.

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

    String input = "Hello Welcome"; can you try the input for above program its giving first repeated character coming wrong

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

      getting correct output 'e' is first repetitive character .... Optional firstRepeatChar = charCount.entrySet().stream().filter((e->e.getValue()>1)).map(e->e.getKey()).findFirst();
      System.out.println("first repeat char: "+firstRepeatChar.orElse(null)); // e= first repeated character

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

    bro If had exam can I drop a message for you