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 
#include 
using namespace std;
void findDistinctCharacters(string random_string) {
   // initializing char count
   map chars;
   // iterating over the string
   for (int i = 0; i 

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.

Updated on: 2020-12-29T11:09:45+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements