Python | Find who tweeted the most

Given a list of tweets, the task is to find the user who has tweeted the most.

Note:
If multiple users are having the same number of tweets, then print all the users in alphabetical order of user names.

Input format:



Read the input from the console.
The first line of input should be the number of test cases
Remaining lines of input should contain each test case input. 

For each test case input:

First-line should contain the number of tweets.
Followed by N lines, each containing the user name
and tweet id separated by a space.

Output format:

Find the user with max number of tweets. 
Print user name and the total number of tweets.

Examples:

Input : 
1
4
sachin tweet_id_1
sehwag tweet_id_2
sachin tweet_id_3
sachin tweet_id_4

Output :
sachin 3

Input :
1
6
sachin tweet_id_1
sehwag tweet_id_2
sachin tweet_id_3
sehwag tweet_id_4
kohli tweet_id_5
kohli tweet_id_6


Output : 
kohli 2
sachin 2
sehwag 2

Code: Python Implementation for finding who tweeted the most

filter_none

edit
close

play_arrow

link
brightness_4
code

# Write Python3 code here
# collection module used counting in dic for value and keys 
from collections import Counter
  
tweet_names = ["sachin tweet_id_1"
               "sehwag tweet_id_2",
               "sachin tweet_id_3",
               "sachin tweet_id_4"
    
uniq_names = [pref_names.split()[0] for
              pref_names in tweet_names]
  
times = Counter(uniq_names)
repeat = times.values()
  
for element in set(repeat):
    dupl = ([(key, value) for
             key, value in sorted(times.items()) if
             value == element])
      
    if len(dupl) > 1:
        for (key, value) in dupl:
            print (key,'',value)
    max_value = max(times.values())
    temp_max_result = [(key, value) for 
                       key, value in sorted(times.items()) if
                       value == max_value]
      
    if temp_max_result != dupl:
        for (key,value) in temp_max_result:
            print (key,'',value)
             

chevron_right


Output :

sachin  3



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.


Article Tags :

Be the First to upvote.


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