Count the number of unique characters in a given String
Given a string, str consisting of lowercase English alphabets, the task is to find the number of unique characters present in the string.
Examples:
Input: str = “geeksforgeeks”
Output: 7
Explanation: The given string “geeksforgeeks” contains 7 unique characters {‘g’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’}.Input: str = “madam”
Output: 3
Approach: The given problem can be solved using the set data structure. The idea is to initialize an unordered set that stores all the distinct characters of the given string. The size of the set after the string is traversed is the required answer.
Below is the implementation of the above approach:
C++
// C++ program of the above approach#include <bits/stdc++.h>using namespace std;// Program to count the number of// unique characters in a stringint cntDistinct(string str){ // Set to store unique characters // in the given string unordered_set<char> s; // Loop to traverse the string for (int i = 0; i < str.size(); i++) { // Insert current character // into the set s.insert(str[i]); } // Return Answer return s.size();}// Driver Codeint main(){ string str = "geeksforgeeks"; cout << cntDistinct(str); return 0;} |
Java
// Java program of the above approachimport java.util.*;class GFG{// Program to count the number of// unique characters in a stringstatic int cntDistinct(String str){ // Set to store unique characters // in the given string HashSet<Character> s = new HashSet<Character>(); // Loop to traverse the string for(int i = 0; i < str.length(); i++) { // Insert current character // into the set s.add(str.charAt(i)); } // Return Answer return s.size();}// Driver Codepublic static void main(String args[]){ String str = "geeksforgeeks"; System.out.print(cntDistinct(str));}}// This code is contributed by sanjoy_62 |
Python3
# Python 3 program of the above approach# Program to count the number of# unique characters in a stringdef cntDistinct(st): # Set to store unique characters # in the given string s = set([]) # Loop to traverse the string for i in range(len(st)): # Insert current character # into the set s.add(st[i]) # Return Answer return len(s)# Driver Codeif __name__ == "__main__": st = "geeksforgeeks" print(cntDistinct(st)) # This code is contributed by ukasp. |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;class GFG{// Program to count the number of// unique characters in a stringstatic int cntDistinct(string str){ // Set to store unique characters // in the given string HashSet<char> s = new HashSet<char>(); // Loop to traverse the string for(int i = 0; i < str.Length; i++) { // Insert current character // into the set s.Add(str[i]); } // Return Answer return s.Count;}// Driver Codepublic static void Main(){ string str = "geeksforgeeks"; Console.Write(cntDistinct(str));}}// This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Program to count the number of // unique characters in a string function cntDistinct(str) { // Set to store unique characters // in the given string let s = new Set(); // Loop to traverse the string for (let i = 0; i < str.length; i++) { // Insert current character // into the set s.add(str[i]); } // Return Answer return s.size; } // Driver Code let str = "geeksforgeeks"; document.write(cntDistinct(str));// This code is contributed by Potta Lokesh </script> |
7
Time Complexity: O(N)
Auxiliary space: O(1)


.png)