Second most repeated word in a sequence in Python
Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence. (Considering no two words are the second most repeated, there will be always a single word).
Examples:
Input : {"aaa", "bbb", "ccc", "bbb",
"aaa", "aaa"}
Output : bbb
Input : {"geeks", "for", "geeks", "for",
"geeks", "aaa"}
Output : for
This problem has existing solution please refer Second most repeated word in a sequence link. We can solve this problem quickly in Python using Counter(iterator) method.
Approach is very simple –
- Create a dictionary using Counter(iterator) method which contains words as keys and it’s frequency as value.
- Now get a list of all values in dictionary and sort it in descending order. Choose second element from the sorted list because it will be the second largest.
- Now traverse dictionary again and print key whose value is equal to second largest element.
Python3
# Python code to print Second most repeated# word in a sequence in Pythonfrom collections import Counterdef secondFrequent(input): # Convert given list into dictionary # it's output will be like {'ccc':1,'aaa':3,'bbb':2} dict = Counter(input) # Get the list of all values and sort it in ascending order value = sorted(dict.values(), reverse=True) # Pick second largest element secondLarge = value[1] # Traverse dictionary and print key whose # value is equal to second large element for (key, val) in dict.items(): if val == secondLarge: print(key) return# Driver programif __name__ == "__main__": input = ['aaa', 'bbb', 'ccc', 'bbb', 'aaa', 'aaa'] secondFrequent(input) |
Output:
bbb
Alternate Implementation :
Python3
# returns the second most repeated wordfrom collections import Counterclass Solution: def secFrequent(self, arr, n): all_freq = dict(Counter(arr)) store = [] for w in sorted(all_freq, key=all_freq.get): # if add key=all_freq.get will sort according to values # without key=all_freq.get will sort according to keys if w not in store: store.append(w) return store[-2] # driver code or main functionif __name__ == '__main__': t = int(input()) for _ in range(t): n = int(input().strip()) arr = input().strip().split(" ") ob = Solution() ans = ob.secFrequent(arr,n) print(ans) contributed by Pratyush Pratap Singh |
bbb
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



