TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!!

My Requirement is.............

i have a input file:
501,501.chan
502,502.anand
503,503.biji
504,504.raja
505,505.chan
506,506.anand
507,507.chan

and my o/p should be

chan->3

i.e. the word which occurs maximum number of times in a file should be displayed..

Its Really Urgent!!!!!!!!!!!!!!!!!!!

A slight weaker one :slight_smile: this will not print 2 names with same max number of occurrence

$ awk -F "." '{arr[$2]+=NF/2} END {for (i in arr) {print i"->",arr[i]}}' maxtime.out| sort -k2 | tail -1

cut -d'.' -f2 filename|sort|uniq -c|sort|tail -1| awk '{print $2"->"$1}'

Awk solution :

> cat f1
501,501.chan
502,502.anand
503,503.biji
503,503.biji
503,503.biji
504,504.raja
505,505.chan
506,506.anand
507,507.chan
>awk  '{      
      a[$NF]++
      if ( a[$NF] >= max )
         max=a[$NF]
      }
  END {
      for ( item in a )
         if ( max == a[item] )
            print item
      }' FS="." f1
biji
chan

Klashxx's solution is a good one, thanks :slight_smile:

Hi,

I am not sure whether this one is more efficient for you.

Please try on your file, any result let all of us here know.

awk 'BEGIN{FS="."}
{
sum[$2]++
}
END{
for (i in sum)
print i"->"sum
}' file > file.t
sort -t">" +1 -n file.t | tail -1
rm file.t