4
Reply

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

Ritesh Singh

Aug 13, 2016
740
0

    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;}

    Ritesh Singh
    August 13, 2016
    1

    string strWords = "one two five one four five one two five two five two five";string[] ary = strWords.Split(' ');

    naveen sharma
    July 04, 2017
    0

    string strWords = "one two five one four five one two five two five two five";string[] ary = strWords.Split(' ');

    naveen sharma
    July 04, 2017
    0

    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;

    naveen sharma
    July 04, 2017
    0