Enter String and find the more occurrence word? Ex. String str = "one two five one four five one two five two five two five" Output:five
Ritesh Singh
public string ReturnRepeatElement(string str) {int i, j;int maxcount = 0;string[] strArray = new string[]{};string _element=string.Empty;strArray = str .Split(' ');for ( i = 0; i < strArray.Length; i++){int count = 0;for ( j = 0; j < strArray.Length;j++ ){if (strArray[i] == strArray[j]){count++;}}if (maxcount < count){maxcount = count;_element=strArray[i];}}return _element;}
string strWords = "one two five one four five one two five two five two five";string[] ary = strWords.Split(' ');
We can also be done same by Linq but most of case interviewer want logic means by looping. var result1 = ary.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(x => new { Key = x.Key, MaxCount = x.Count() }).FirstOrDefault();string WordMaxTime = result1.Key;int Maxcount = result1.MaxCount;