Given a file, find the ten most frequently occurring words as efficiently as possible

后端 未结 15 1699
予麋鹿
予麋鹿 2020-12-12 13:26

This is apparently an interview question (found it in a collection of interview questions), but even if it\'s not it\'s pretty cool.

We are told to do this efficien

15条回答
  •  心在旅途
    2020-12-12 14:07

    I have done in C# like this(a sample)

    int wordFrequency = 10;
    string words = "hello how r u u u u  u  u u  u  u u u  u u u u  u u u ? hello there u u u u ! great to c u there. hello .hello hello hello hello hello .hello hello hello hello hello hello ";            
    
    var result = (from word in words.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                              group word by word into g
                              select new { Word = g.Key, Occurance = g.Count() }).ToList().FindAll(i => i.Occurance >= wordFrequency);
    

提交回复
热议问题