Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Odd Frequency Characters
In Python, fetching characters with odd frequencies from a given string can be a very common task in text processing and data analysis. In this article, we will learn various methods to fetch the odd frequency characters of a string in Python.
Using a Dictionary
Dictionaries are very convenient when there is a need to keep track of the frequencies of elements. We can manually count each character's occurrences and filter those with odd frequencies.
Approach
To get the odd frequency elements, we will iterate through the entire string, and for each character in the string, we will increment the character count in the dictionary. At the end of the iteration, we will be left with a dictionary having the count of occurrences of each character, then we will display the characters having the odd frequency.
Example
st = "TutorialsPoint"
occurrences = {}
result = []
# Populating dictionary with a count of each character
for char in st:
occurrences[char] = occurrences.get(char, 0) + 1
# Fetching characters with odd frequency
for char, count in occurrences.items():
if count % 2 != 0:
result.append(char)
print("Odd frequency characters:", result)
Odd frequency characters: ['T', 'u', 'r', 'a', 'l', 's', 'P', 'n']
Using Counter from collections
Python's Counter class from the collections module provides an easy way to count the frequency of elements in an iterable, including characters in a string. This approach simplifies the counting process compared to manual dictionary handling.
Example
from collections import Counter
st = "TutorialsPoint"
occurrences = Counter(st)
result = []
for char, count in occurrences.items():
if count % 2 != 0:
result.append(char)
print("Odd frequency characters:", result)
print("Character frequencies:", dict(occurrences))
Odd frequency characters: ['T', 'u', 'r', 'a', 'l', 's', 'P', 'n']
Character frequencies: {'T': 1, 'u': 1, 't': 2, 'o': 2, 'r': 1, 'i': 2, 'a': 1, 'l': 1, 's': 1, 'P': 1, 'n': 1}
Using List Comprehension with count()
List comprehension provides a way to create lists based on existing lists or other iterables. We can use list comprehension along with the count() method to count the frequency of each character in the string and construct a list of characters with odd frequencies.
Example
st = "TutorialsPoint"
unique_chars = set(st)
result = [char for char in unique_chars if st.count(char) % 2 != 0]
print("Odd frequency characters:", result)
Odd frequency characters: ['T', 'l', 'P', 'a', 'n', 's', 'r', 'u']
Using Nested Loops
We can use a nested loop to find the frequency of each character. For each unique character, we iterate through the entire string and count its occurrences. If the count is odd, we add it to the result list.
Example
st = "TutorialsPoint"
# Getting distinct characters from string
unique_chars = set(st)
result = []
for char in unique_chars:
count = 0
for c in st:
if char == c:
count += 1
if count % 2 == 1:
result.append(char)
print("Odd frequency characters:", result)
Odd frequency characters: ['T', 's', 'P', 'r', 'n', 'a', 'u', 'l']
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Dictionary | O(n) | O(k) | Manual control over counting |
| Counter | O(n) | O(k) | Clean, readable code |
| List Comprehension | O(n×k) | O(k) | Concise oneliner |
| Nested Loops | O(n×k) | O(k) | Educational purposes |
Where n = string length, k = unique characters
Conclusion
The Counter method provides the cleanest and most efficient solution for finding odd frequency characters. For performancecritical applications with large strings, use dictionary or Counter methods as they offer O(n) time complexity compared to the O(n×k) complexity of other approaches.
