Index of first repeated character in a string.

Let me break down and elaborate on the Java code snippet you've provided. This code is designed to find the first repeated character in a string and then print its index.

Code Explanation

1. String Initialization

java
String repeadted = "adffda";
  • A string repeadted is initialized with the value "adffda". The word seems like a typo, and it probably meant "repeated."

2. Creating a Set to Track Characters

java
Set<Character> set1 = new HashSet<>();
  • A HashSet named set1 is created to store characters from the string repeadted.
  • The HashSet data structure is chosen because it does not allow duplicate elements, making it perfect for detecting repeated characters.

3. Finding the First Repeated Character

java
char ch1 = (char) repeadted.chars() .filter(c -> !set1.add((char) c)) .findFirst() .getAsInt();
  • repeadted.chars(): This method converts the string repeadted into an IntStream of its character codes. Each character in the string is represented by its corresponding ASCII value.
  • filter(c -> !set1.add((char) c)):
    • The filter method is used to process each character's ASCII value.
    • (char) c: Casts the integer ASCII value c back to a char.
    • !set1.add((char) c): Attempts to add the character to set1.
      • The add method returns true if the character is not already in the set and successfully adds it.
      • If the character is already in the set (indicating it’s a duplicate), add returns false.
    • The filter method only retains those characters that return false from set1.add((char) c)—i.e., the characters that are duplicates.
  • findFirst(): Returns an OptionalInt describing the first element of this stream (the first duplicated character found).
  • getAsInt(): Retrieves the integer value of the OptionalInt, which is the ASCII code of the first repeated character.
  • (char) ...: Casts the integer ASCII code back to a char, assigning it to ch1.

4. Printing the Index of the First Repeated Character

java
System.out.println(repeadted.indexOf((char) ch1));
  • repeadted.indexOf((char) ch1): Finds the index of the first occurrence of the character ch1 in the string repeadted.
  • System.out.println(...): Prints the index to the console.

What Does This Program Do?

  1. Initialization: The program initializes a string, "adffda".
  2. Finding the First Repeated Character:
    • It uses a HashSet to keep track of characters it has seen.
    • As it iterates through each character in the string (using chars()), it tries to add each character to the set.
    • If adding a character fails (because the character is already in the set), that character is identified as the first repeated character.
  3. Finding and Printing the Index:
    • The program then finds the index of this first repeated character in the original string using indexOf and prints it.

Example Walkthrough

For the string "adffda":

  1. Characters processed:
    • 'a' → added to set.
    • 'd' → added to set.
    • 'f' → added to set.
    • 'f' → not added (already in set). First repeated character.
  2. Find index:
    • repeadted.indexOf('f') → 2 (zero-based index).

Output:

2

The output 2 is printed, which is the index of the first occurrence of the repeated character 'f' in the string "adffda".

Summary

This code snippet efficiently finds the first repeated character in a string and prints its index using Java 8 features like streams, HashSet, and lambda expressions.

String repeadted="adffda"; Set set1=new HashSet<>(); char ch1=(char) repeadted.chars() .filter(c->!set1.add((char)c)) .findFirst() .getAsInt(); System.out.println(repeadted.indexOf((char)ch1));

Comments

Popular posts from this blog

First Repeating character in a string

Usage of map,max,sorted,comparing java 8 methods