Program to find the frequency of characters

Explanation

In this program, we need to find the frequency of each character present in the word.

To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

For example: Frequency of p in above string is 2.

Algorithm

  1. Define a string.
  2. Define an array freq with the same size of the string.
  3. Two loops will be used to count the frequency of each character. Outer loop will be used to select a character and initialize element at corresponding index in array freq with 1.
  4. Inner loop will compare the selected character with rest of the characters present in the string.
  5. If a match found, increment element in freq by 1 and set the duplicates of selected character by '0' to mark them as visited.
  6. Finally, display the character and their corresponding frequencies by iterating through the array freq.

Solution

Python

Output:

 Characters and their corresponding frequencies
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

C

Output:

Characters and their corresponding frequencies: 
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

JAVA

Output:

Characters and their corresponding frequencies
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1 

C#

Output:

Characters and their corresponding frequencies: 
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1 

PHP

Output:

Characters and their corresponding frequencies:
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

Next Topic#




Latest Courses