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 –



  1. Create a dictionary using Counter(iterator) method which contains words as keys and it’s frequency as value.
  2. 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.
  3. Now traverse dictionary again and print key whose value is equal to second largest element.
filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to print Second most repeated
# word in a sequence in Python
from collections import Counter
  
def 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.iteritems():
        if val == secondLarge:
            print key
            return
  
# Driver program
if __name__ == "__main__":
    input = ['aaa','bbb','ccc','bbb','aaa','aaa']
    secondFrequent(input)

chevron_right


Output:

bbb


Alternate Implementation :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Second most repeated word in a sequence in Python
  
def secondFrequent(input):
    from collections import Counter
  
    # this sorts from most common to least common to least common
    c = Counter(input
       
    # c.most_common()[1] prints ('bbb',2) 
    # c.most_common()[1][0] prints output: bbb
    print(c.most_common()[1][0])
  
# Driver code
input = ['aaa','bbb','ccc','bbb','aaa','aaa']
secondFrequent(input)

chevron_right


Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Midhilesh

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.