- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
First non-repeating character using one traversal of string in C++
In this tutorial, we are going to learn how to find the first non-repeating character in the given string. Let's see an example.
Input −tutorialspoint
Output −u
Let's see the steps to solve the problem.
Initialize the string.
Initialize a map char and array to store the frequency of the characters in the string.
Iterate over the string.
Find the frequency of each character and store them in the map.
Store the index of the character as well.
Iterate over the character frequencies in the map.
Print the first character with the frequency 1.
Example
Let's see the code.
#include <bits/stdc++.h> #include <map> using namespace std; void findDistinctCharacters(string random_string) { // initializing char count map<char, int[2]> chars; // iterating over the string for (int i = 0; i < random_string.size(); ++i){ chars[random_string[i]][0]++; chars[random_string[i]][1] = i; } int char_index = INT_MAX; // printing the first char with frequency 1 for (auto item: chars) { // checking the frequency if (item.second[0] == 1) { char_index = min(char_index, item.second[1]); } } // printing the first char with frequency 1 cout << random_string[char_index] << u; } int main() { findDistinctCharacters("tutorialspoint"); return 0; }
Output
If you run the above code, then you will get the following result.
u
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Finding the first non-repeating character of a string in JavaScript
- Finding first non-repeating character JavaScript
- Find the last non repeating character in string in C++
- How to find its first non-repeating character in a given string in android?
- Find first repeating character using JavaScript
- Return index of first repeating character in a string - JavaScript
- Find the first non-repeating character from a stream of characters in Python
- Detecting the first non-repeating string in Array in JavaScript
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Repeating each character number of times their one based index in a string using JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- First non-repeating in a linked list in C++
- Maximum consecutive repeating character in string in C++
- Python program to Find the first non-repeating character from a stream of characters?
- Java program to Find the first non-repeating character from a stream of characters

Advertisements