Java program to find the first repeating character in a string

Java program to find the first repeating character in a string:

In this post, we will learn how to find the first repeating character in a string in Java. For example, for the string hello world, l is the first repeating character. We will write a program that will take a string as the input from the user and it will print the first repeating character of that string.

We can either use two loops or a hashmap to solve this problem. I will show you both ways to write the programs.

Method 1: By using two loops:

We can use two loops to find the first repeating character in a given string.

  • Use two loops, one outer loop and one inner loop.
  • The outer loop will run from the first to the last character of the string. For each character it points, the inner loop will iterate for all other characters to the right of it. It will check if the outer loop character is available at any other place in the string or not. If yes, it returns that character as it is the first repeating character and quit from the loops.

Let’s write down the program:

import java.util.Scanner;

class Main {

    private static int getFirstRepeatingCharIndex(String str) {
        for (int i = 0; i < str.length(); i++) {
            for (int j = i + 1; j < str.length(); j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        String str;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string: ");
        str = sc.nextLine();

        int index = getFirstRepeatingCharIndex(str);

        if (index == -1) {
            System.out.println("No repeating character found");
        } else {
            System.out.println(str.charAt(index) + " is the first repeating character.");
        }
    }
}

Here,

  • getFirstRepeatingCharIndex method is used to get the index of the first repeating character in the given string. This method takes a string as the parameter and returns the index of the first repeating character.
    • It uses two for loops. The outer loop runs for each character of the string one by one.
    • The inner loop runs for all characters to the right of the character pointed by the inner loop.
    • If it finds any character which is pointed by the inner loop is equal to the outer loop, it returns that index.
    • If no repeating character found, it returns -1.
  • Based on the return value of getFirstRepeatingCharIndex method, it prints a message to the user.

If you run this program, it will print output as like below:

Enter a string: 
hel o
No repeating character found

Enter a string: 
hello
l is the first repeating character.

Enter a string: 
hello world
l is the first repeating character.

Method 2: By using a HashSet:

We can also use a hashset to find the first repeating character of a string in Java. The idea is to keep adding all characters of the string to a hashset one by one. If a character is already added to the hashset, it will be the repeated character.

Below is the complete program:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

class Main {

    private static int getFirstRepeatingCharIndex(String str) {
        Set<Character> charSet = new HashSet<>();

        for (int i = 0; i < str.length(); i++) {
            if (!charSet.add(str.charAt(i))) {
                return i;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        String str;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string: ");
        str = sc.nextLine();

        int index = getFirstRepeatingCharIndex(str);

        if (index == -1) {
            System.out.println("No repeating character found");
        } else {
            System.out.println(str.charAt(index) + " is the first repeating character.");
        }
    }
}

Here,

  • We created a new Set charSet that can hold character values.
  • The for loop is used to iterate through the characters of the string. It uses add to add a character to the hashset. This method returns True if the character is not added to the hashset. Else, if it is already added, it returns False.
  • If the return value of add is False, return the index. This is the index of the first repeating character.
  • Once the loop ends, return -1.

If you run this program, it will give similar result.

Example of java program to find the first repeating character of a string

You might also like: