Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find a unique character in a string using java?
In this article, we will learn how to find a unique character in a string in Java. A unique character is a character that occurs only once in the string, while all others can occur multiple times.
We can solve this problem using the following ways:
Using brute force method
To find a unique character in a given string, we will use a brute force approach. In the brute force approach, we will use two loops to compare each character to the rest of the string.
Let's look at the steps:
- Initiate a for loop with i = 0 and run it until i is less than the string length.
- Use charAt() method to get the character at the current index.
- Take a isUnique boolean variable and set it true.
- Within the first loop, start another nested for loop with j = 0, and it goes till j is less than the string length.
- Check if the character at index
imatches the character at indexjand ensureiis not equal toj. - If it does match immediately set isUnique to false and break the inner loop.
- Once you are done with traversing the whole inner loop, check if isUnique is still true, and if so, print the character.
Example
Following is the Java program to find a unique character in a string. You are given a string that has a duplicate character except one character, we need to find that.
public class FindUniqueChar{
public static void main(String args[]){
String s = "abbbccdd";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
boolean isUnique = true;
for(int j = 0; j < s.length(); j++){
if(i != j && ch == s.charAt(j)){
isUnique = false;
break;
}
}
if(isUnique){
System.out.println("The unique character is [" + ch + "]");
}
}
}
}
Output
Following is the output of the above code :
The unique character is [a]
Using HashMap
We can also use HashMap, which is part of Java Collections Framework. We will use a HashMap to store the frequency of each character in the string, and will check if the character has a single frequency. In this way, we will get a unique character in a string.
Steps to find a unique character in a string using a HashMap.
- Initiate a HashMap, and store the frequency of characters.
- Loop through the HashMap, check if any value is equal to 1. If so, print the respective key.
Example
Following is the Java program to find a unique character in a string using a HashMap.
import java.util.*;
public class UniqueChar{
public static void main(String args[]){
String s = "bbssdcd";
HashMap<Character, Integer> map = new HashMap<>();
for(char ch :s.toCharArray()){
if(!map.containsKey(ch)){
map.put(ch,1);
}else{
map.put(ch, map.get(ch)+1);
}
}
for(Map.Entry<Character, Integer> i : map.entrySet()){
if(i.getValue() == 1){
System.out.println(i.getKey());
}
}
}
}
Example
Following is the output:
c