Probably less performant than BlueTrin's answer BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
static int CountMode<T>(IEnumerable<T> str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Note the similarity to mjolka's LINQ solution mjolka's LINQ solution. This differs in how the empty string case is handled (since there is no MaxOrDefault()
). The ternary version is shorter; however, the LINQ version can work for all IEnumerable<T>
without multiple enumeration.
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
static int CountMode<T>(IEnumerable<T> str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Note the similarity to mjolka's LINQ solution. This differs in how the empty string case is handled (since there is no MaxOrDefault()
). The ternary version is shorter; however, the LINQ version can work for all IEnumerable<T>
without multiple enumeration.
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
static int CountMode<T>(IEnumerable<T> str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Note the similarity to mjolka's LINQ solution. This differs in how the empty string case is handled (since there is no MaxOrDefault()
). The ternary version is shorter; however, the LINQ version can work for all IEnumerable<T>
without multiple enumeration.
Probably less performant than BlueTrin's answerBlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
public static int CountModeCountMode<T>(stringIEnumerable<T> str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Note the similarity to mjolka's LINQ solution . This differs in how the empty string case is handled (since there is no MaxOrDefault()
). The ternary version is shorter; however, the LINQ version can work for all IEnumerable<T>
without multiple enumeration.
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
public static int CountMode(string str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
static int CountMode<T>(IEnumerable<T> str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}
Note the similarity to mjolka's LINQ solution . This differs in how the empty string case is handled (since there is no MaxOrDefault()
). The ternary version is shorter; however, the LINQ version can work for all IEnumerable<T>
without multiple enumeration.
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
public static charint GetMostFrequentElementCountMode(string sstr)
{
return sstr.GroupBy(c => c).OrderByDescendingSelect(g => g.Count())
.Select(g => g.KeyOrderBy(freq=>freq).FirstOrDefaultLastOrDefault();
}
Probably less performant than BlueTrin's answer, but worth considering.
public static char GetMostFrequentElement(string s)
{
return s.GroupBy(c => c).OrderByDescending(g => g.Count())
.Select(g => g.Key).FirstOrDefault();
}
Probably less performant than BlueTrin's answer, but worth considering.
Edit: I originally misunderstood the problem to be looking for the character that occurred most frequently (i.e. the "mode" of the data set). Actually, the OP wants to know how many times the mode is repeated in the data set.
public static int CountMode(string str)
{
return str.GroupBy(c => c).Select(g => g.Count())
.OrderBy(freq=>freq).LastOrDefault();
}