WAP To Find Out First Repeated Character In A String

Find Out First Repeated Character In A String: Welcome to the captivating world of string manipulation and coding challenges! In this exciting topic, we’ll explore the intriguing problem of finding the first repeated character in a given string. Imagine a scenario where you’re presented with a text, and your task is to swiftly identify the first character that appears more than once in the string. This seemingly simple yet powerful problem has applications in various real-world scenarios, such as detecting duplicate entries in databases, validating user inputs, and optimizing search algorithms.

Throughout this journey, we’ll walk you step-by-step through different approaches to efficiently solve this problem using Java. We’ll dive into fundamental programming concepts, such as loops, conditional statements, and data structures while gaining valuable insights into algorithm design. Whether you’re a Java beginner or a seasoned developer, this exploration promises to enhance your problem-solving skills and deepen your understanding of string manipulation.

Our goal is to empower you with the knowledge and techniques to tackle this challenge with elegance and confidence. We’ll demonstrate how simple yet effective coding can lead to powerful solutions. Are you ready to embark on this thrilling adventure to find the first repeated character in a string? Let’s unravel the mysteries of string analysis and become proficient in text processing.

Find Out First Repeated Character In A String

package com.softwaretestingo.interviewprograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InterviewPrograms95 
{
	//Write a program to find out First repeated character in a string
	private static char findFirstRepeatedChar(String str) 
	{
		int n = str.length();
		char ans = ' ';
		int index = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) 
		{
			char temp = str.charAt(i);

			// Checking that character in temp repeats or not by running a for loop
			for (int j = i + 1; j < n; j++)
			{
				if (str.charAt(j) == temp)
				{

					// if the index where it repeated is less than the index of the previously
					// repeated character then store this character in ans variable
					// and its index where it repeated in index variable
					if (j < index)
					{
						index = j;
						ans = str.charAt(j);
					}
				}
			}
		}
		return ans;
	}

	public static void main(String[] args) throws IOException 
	{
		String str;

		// create an object of Scanner
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string");
		str=br.readLine();

		System.out.println("The First Repeated Character: "+findFirstRepeatedChar(str));
		br.close();
	}
}

Output

Enter the string
softwaretestingo
The First Repeated Character: t

Alternative Way 1:

package com.softwaretestingo.interviewprograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class InterviewPrograms95_1 
{
	//Write a program to find out First repeated character in a string
	private static char findFirstRepeatedChar(String str) 
	{
		char[] arr = str.toCharArray();

		HashSet<Character> h = new HashSet<>();

		for (int i=0; i<=str.length()-1; i++)
		{
			char c = arr[i];

			if (h.contains(c))
				return c;

			else
				h.add(c);
		}
		return 0;
	}

	public static void main(String[] args) throws IOException 
	{
		String str;

		// create an object of Scanner
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string");
		str=br.readLine();

		System.out.println("The First Repeated Character: "+findFirstRepeatedChar(str));
		br.close();
	}
}

Output

Enter the string
softwaretestingo
The First Repeated Character: t

Alternative Way 2:

package com.softwaretestingo.interviewprograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InterviewPrograms95_2 
{
	//Write a program to find out First repeated character in a string
	private static String findFirstRepeatedChar(String str) 
	{
		// code here
		int a[] = new int[26];

		for (int i = 0; i<str.length(); i++) {
			char ch = str.charAt(i);

			if (a[ch - 'a'] != 0)
				return Character.toString(ch);
			else
				a[ch - 'a']++;
		}
		return "-1";
	}

	public static void main(String[] args) throws IOException 
	{
		String str;

		// create an object of Scanner
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string");
		str=br.readLine();

		System.out.println("The First Repeated Character: "+findFirstRepeatedChar(str));
		br.close();
	}
}

Output

Enter the string
softwaretestingo
The First Repeated Character: t

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment