- 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
How to obtain the highest occurring character in a String using C#?
The highest occurring character in a string is one that occurs most number of times. This can be demonstrated using the following example.
String: apples are red The highest occurring character in the above string is e as it occurs 3 times, which is more than the occurrence of any other character.
A program that obtains the highest occurring character in a string using C# is given as follows.
Example
using System; namespace charCountDemo { public class Example { public static void Main() { String str = "abracadabra"; int []charCount = new int[256]; int length = str.Length; for (int i = 0; i < length; i++) { charCount[str[i]]++; } int maxCount = -1; char character = ' '; for (int i = 0; i < length; i++) { if (maxCount < charCount[str[i]]) { maxCount = charCount[str[i]]; character = str[i]; } } Console.WriteLine("The string is: " + str); Console.WriteLine("The highest occurring character in the above string is: " + character); Console.WriteLine("Number of times this character occurs: " + maxCount); } } }
Output
The output of the above program is as follows.
The string is: abracadabra The highest occurring character in the above string is: a Number of times this character occurs: 5
Now, let us understand the above program.
The string str is abracadabra. A new array charCount is created which is of size 256 and shows all the characters in the ASCII table. Then the string str is traversed using a for loop and the value in charCount is incremented corresponding to the character in the string. This can be seen in the following code snippet.
String str = "abracadabra"; int []charCount = new int[256]; int length = str.Length; for (int i = 0; i < length; i++) { charCount[str[i]]++; }
The integer maxCount stores the maximum count and character is the char value that occurs the maximum times. The values of maxCount and character can be determined using a for loop. This can be seen in the following code snippet.
int maxCount = -1; char character = ' '; for (int i = 0; i < length; i++) { if (maxCount < charCount[str[i]]) { maxCount = charCount[str[i]]; character = str[i]; } }
Finally, the values of str, maxCount and character are displayed. This can be seen in the following code snippet.
Console.WriteLine("The string is: " + str); Console.WriteLine("The highest occurring character in the above string is: " + character); Console.WriteLine("Number of times this character occurs: " + maxCount);
- Related Articles
- Maximum occurring character in an input string using C++
- How to find the shortest distance to a character in a given string using C#?
- How to find the longest distance to a character in a given string using C#?
- How to extract the first highest occurring value in an R data frame column?
- The most occurring number in a string using Regex in python
- Find the first repeated character in a string using C++.
- How to find a unique character in a string using java?
- Find one extra character in a string using C++.
- How to find the first character of a string in C#?
- How to convert a single character to string in C++?
- How to delete a character from a string using python?
- PHP – How to return the character count of a string using iconv_strlen()?
- Count substrings with each character occurring at most k times in C++
- Python program to find the most occurring character and its count
- Find the index of the first unique character in a given string using C++
