[SOLVED] Shell script to get the most occurring entry from a list
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So what have you tried? It looks reasonably trivial and could be done with a myriad of applications or languages.
You may wish to provide details on what you would like to use and where you are getting stuck?
Declare an associative array. Also known as a dictionary, map or hash table, depending on the language. Then loop through the list of addresses. For each one, if it's not in the associative array, then add it and associate a count of zero with it. If it's already there, then increment its associated count. Then go through the associative array and find the address with the largest count.
from collections import defaultdict
d=defaultdict(int)
for line in open("file"):
d[line.rstrip()]+=1
m=max(d.values())
for i,j in d.iteritems():
if m==j:
print i,j
@crts - I came up with a similar solution to your but not sure how your test works?
Hi,
good catch! What I was actually thinking of was to store the biggest occurrence just as you did. I previously had another solution where res
did hold the actual biggest occurrence.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.