• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to find the first non-repeated character in a String?

WAP to find the first non-repeated character in a String?

Last Updated on: July 26, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Find the first non-repeated character in a word in Java

The program InterviewPrograms49 aims to find the first non-repeated character in a given string (s). The program uses two nested loops to compare each character of the string with every other character to count the number of occurrences (distinct) of each character.

Find the first non-repeated character in a word in Java

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms49 
{
	/* wap to find the first non repeating character. */
	public static void main(String[] args) 
	{
		String s="minimum";
		int distinct=0;

		for(int i=0;i<s.length();i++)
		{
			for(int j=0;j<s.length();j++)
			{
				if(s.charAt(i)==s.charAt(j))
				{
					distinct++;
				}
			}
			if(distinct==1)
			{
				System.out.println(s.charAt(i)+"--"+distinct+"\n");
				break;
			}
			String d=String.valueOf(s.charAt(i)).trim();
			s.replaceAll(d,"");
			distinct=0;
		}
	}
}

Output

n--1

Here’s how the program works:

  1. First, the given string s is initialized with the value “minimum”.
  2. The variable distinct is used to count the occurrences of each character and is initially set to 0.
  3. The program then enters a loop that iterates through each character of the string using the index i.
  4. Inside the outer loop, there is another loop that uses index j to compare each character with every other character in the string.
  5. If the characters at indices i and j are the same, the distinct variable is incremented.
  6. After the inner loop finishes, the program checks if the distinct count is equal to 1, indicating that the character is non-repeating.
  7. If the condition is true, the program prints the non-repeating character along with its count using System.out.println().
  8. The program then breaks out of the loop to end the execution, as the first non-repeating character is found.
  9. If the condition is false (meaning the character is repeating), the program removes all occurrences of the current character from the string s using replaceAll().
  10. The distinct count is reset to 0.
  11. The program continues with the next character in the outer loop until it finds the first non-repeating character and prints its count.

In this specific case with the input string “minimum”, the program will output “n–1”, indicating that the first non-repeating character is ‘n’ and it occurs only once in the string.

    WAP to Reverse String Based on Commas
    WAP to Reverse String Based on Commas
    Product of Array Except Itself Program
    Product of Array Except Itself
    WAP to Replace Last Two Special Character With Dots
    WAP to Replace Last Two Special Character With Dots
    WAP to Split a String on Uppercase Characters
    WAP to Split a String on Uppercase Characters
    Java Program Interview Concatenate Characters From Each String
    WAP to Concatenate Characters From Each String
    Reverse Number In Java
    Reverse Number In Java
    WAP to Check Consecutive Character Sequence in an Array
    WAP to Remove Consecutive Duplicate Characters from String
    Sort A String According To The Frequency Of Characters
    WAP to Sort A String According To The Frequency Of...
    WAP to Remove Characters That Appear More Than K Times
    WAP to Remove Characters That Appear More Than K Times
    Convert Unique Uppercase to Lowercase Letter
    Convert Only Uppercase to Lowercase Letter

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers