- 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
C# program to find the most frequent element
Let’s say our string is −
String s = "HeathLedger!";
Now create a new array.
int []cal = new int[maxCHARS];
Create a new method and pass both the string and the new array in it. Find the maximum occurrence of a character.
static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; }
Let us see the complete code −
Example
using System; class Demo { static int maxCHARS = 256; static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; } public static void Main() { String s = "thisisit!"; int []cal = new int[maxCHARS]; calculate(s, cal); for (int i = 0; i < maxCHARS; i++) if(cal[i] > 1) { Console.WriteLine("Character "+(char)i); Console.WriteLine("Occurrence = " + cal[i] + " times"); } } }
Output
Character i Occurrence = 3 times Character s Occurrence = 2 times Character t Occurrence = 2 times
- Related Articles
- Program to find frequency of the most frequent element in Python
- Program to find second most frequent character in C++
- Find the second most frequent element in array JavaScript
- Write a program in C++ to find the most frequent element in a given array of integers
- Most frequent element in an array in C++
- Find most frequent element in a list in Python
- Program to find out the index of the most frequent element in a concealed array in Python
- Python program to find Most Frequent Character in a String
- Program to find most frequent subtree sum of a binary tree in Python
- Most Frequent Subtree Sum in C++
- Most Frequent Number in Intervals in C++
- Write a program in C++ to find the top K frequent element in an array of integers
- Find Second most frequent character in array - JavaScript
- Python program for most frequent word in Strings List
- Python Program to crawl a web page and get most frequent words

Advertisements