Given a string, find the first non-repeating character in it.
Solution:
- Using Array-
- Create an arrays (count[]) of size 256 (Total ASCII codes).
- Read the input string character by character and increament values of count[] at index corresponding to the ASCII value of the current character.
- To find the first non-repeating character, again read through the input, character by character and check if it's count is equal to 1 from the count[] array.
- If count[] is equal to 1, print character and exit.
- Else print no "non repeating character found".
- Using HashMap-
- Create a HashMap count<Character, Integer>.
- Read the input string character by character,
- If character already present, increament it's count
- Else insert the character with count as 1.
- To find the first non-repeating character, again read through the input, character by character and check if it's count in the HashMap is equal to 1,
- If equal to 1, print character and exit.
- Else print no "non repeating character found".
Java code-
Using Array-
public class FirstNonRepeatingCharacterUsingArray
{
public static void main(String[] args)
{
int count[] = new int[256];
String s = "MuscleJavaMuscle";
for(int i=0; i<s.length(); i++)
{
count[ s.charAt(i) ]++ ;
}
System.out.println("First non-repeating character- ");
for(int i=0; i<s.length(); i++)
{
if(count[ s.charAt(i) ] == 1)
{
System.out.println(s.charAt(i));
System.exit(0); //exit program if required
{
public static void main(String[] args)
{
int count[] = new int[256];
String s = "MuscleJavaMuscle";
for(int i=0; i<s.length(); i++)
{
count[ s.charAt(i) ]++ ;
}
System.out.println("First non-repeating character- ");
for(int i=0; i<s.length(); i++)
{
if(count[ s.charAt(i) ] == 1)
{
System.out.println(s.charAt(i));
System.exit(0); //exit program if required
//output is found
}
}
/*this part of code will run if
the first non repeating character is not found*/
System.out.println("Not found");
}
}
}
}
/*this part of code will run if
the first non repeating character is not found*/
System.out.println("Not found");
}
}
Using HashMap-
public class FirstNonRepeatingCharUsingHashMap
{
public static void main(String[] args)
{
HashMap<Character, Integer> count = new HashMap<>();
String s = "MuscleJavaMuscle";
for(int i=0; i<s.length(); i++)
{
if(!count.containsKey(s.charAt(i))) //if not present in
{
public static void main(String[] args)
{
HashMap<Character, Integer> count = new HashMap<>();
String s = "MuscleJavaMuscle";
for(int i=0; i<s.length(); i++)
{
if(!count.containsKey(s.charAt(i))) //if not present in
//HashMap
count.put(s.charAt(i), 1); //put the char and
count.put(s.charAt(i), 1); //put the char and
//its count as '1'
else //if present, increase it count
count.replace(s.charAt(i),count.get(s.charAt(i))+1);
}
//to print
for(int i=0; i<s.length(); i++)
{
if(count.get(s.charAt(i)) == 1)
{
System.out.print("First non-repeating "
+ "character- " + s.charAt(i));
System.exit(0);
}
}
/*this part of code will run if
the first non repeating character is not found*/
System.out.println("Not found");
}
}
else //if present, increase it count
count.replace(s.charAt(i),count.get(s.charAt(i))+1);
}
//to print
for(int i=0; i<s.length(); i++)
{
if(count.get(s.charAt(i)) == 1)
{
System.out.print("First non-repeating "
+ "character- " + s.charAt(i));
System.exit(0);
}
}
/*this part of code will run if
the first non repeating character is not found*/
System.out.println("Not found");
}
}
Output-
First non-repeating character- J