-3
  String ss = "lijo is lijom is lijo fdf"; //here two words "is" and "lijo" is repeating two times so my output will be 
                          output   : lijo 
                                      is

I tried Like this

 var count=words.GroupBy(g=>g).Max(k=>k.Count());
//finding the max repeating elements count =2


 var res = words.GroupBy(s => s).Where(g => g.Count() == count).Select(s => s);

here i have tried by first finding the max count of repeating words. and assign this value in another query.

i need to know if we can write both this queries together as one query. as a subquery how to do this in a different way ? using subquery or any other easy method?

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • why down voting...what wrong with question please atleast give a comment – Lijo Jul 09 '14 at 03:37
  • this question is not a repetition .using max property we cannot find the repeating words..max will check with the ascci value of string... – Lijo Jul 10 '14 at 03:10
  • please the check question clearly those who are saying its repeating..i have goggled a lot but i didnt get an answer...that's why i asked here..max property will always return one value.but here i need all the words that are repeating maximum. – Lijo Jul 10 '14 at 03:22
  • Please consider upvoting, commenting or marking as answer for any answer that helped you. – crthompson Jul 23 '14 at 04:04

2 Answers2

2

There is an overload of group by that can easily give you your counts.

String ss = "lijo is lijom is lijo fdf";
var words = ss.Split();
var query = words.GroupBy(
            word => word,
            (key, counts) => new
            {
                Word = key,
                Count = counts.Count()
            });

To finish this off, I'd recommend using a MaxBy extension.

Community
  • 1
  • 1
crthompson
  • 15,653
  • 6
  • 58
  • 80
0

Try this:

var l = words.GroupBy(x => x).Select(y => new{key = y.Key, count = y.Count()}).ToList();
var max = l.OrderByDescending(y=>y.count).First().count;
var result = l.Where(x => x.count == max).Select(y=>y.key);

l simply gets the pairs of words and their frequencies. max is the maximum count from l. result will give you the words which have the maximum frequency.

Demo

shree.pat18
  • 21,449
  • 3
  • 43
  • 63