在C ++中按出现频率打印具有奇数频率的字符

在这个问题上,我们为用户提供了字符串str。而且我们仅需打印出现频率为奇数的那些字符。

为了解决这个问题,我们必须找到字符串中字符出现的总频率。并且仅打印出现频率奇数的字符串字符。

让我们举个例子来更好地理解这个话题-

Input : adatesaas.
Output : dte

说明-出现频率的字符为-

一种4
d1
Ť1
Ë1
s2

奇数频率的字符是d,t,e。

算法

现在让我们尝试创建一种算法来解决这个问题-

Step 1 : Traverse the string and count the number of occurrences on characters of the string in an array.
Step 2 : Traverse the frequency array and print only those characters whose frequency of occurrence is odd.

示例

让我们基于此算法创建一个程序-

#include <bits/stdc++.h>
using namespace std;
int main(){
   string str = "asdhfjdedsa";
   int n = str.length();
   int frequency[26];
   memset(frequency, 0, sizeof(frequency));
   for (int i = 0; i < n; i++)
      frequency[str[i] - 'a']++;
   for (int i = 0; i < n; i++) {
      if (frequency[str[i] - 'a'] % 2 == 1) {
         cout << str[i]<<" , ";
      }
   }
   return 0;
}

输出结果

d , h , f , j , d , e , d