Ritesh Singh
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
By Ritesh Singh in .NET on Aug 13 2016
  • Ritesh Singh
    Aug, 2016 13

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

    • 1
  • Dinuka Hettiarachchi
    Nov, 2021 18

    using System;

    namespace DemoApp
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.Write(“Output: “ + GetMaxWord(“one two five one four five one two five two five two five”));
    }

    1. public static string GetMaxWord(string word)
    2. {
    3. string[] wordlist = word.Split(' ');
    4. var diswords = wordlist.Length;
    5. double[] number = new double[diswords];
    6. var j = 0;
    7. foreach (var item in wordlist)
    8. {
    9. for (int i = 0; i < diswords; i++)
    10. {
    11. if (item.ToLower() == wordlist[i].ToString().ToLower())
    12. {
    13. number[j] = number[j] + 1;
    14. }
    15. }
    16. j++;
    17. }
    18. double k = 0;
    19. var l = -1;
    20. for (int i = 0; i < number.Length; i++)
    21. {
    22. if (k < number[i])
    23. {
    24. k = number[i];
    25. l = i;
    26. }
    27. }
    28. return wordlist[l];
    29. }
    30. }

    }

    //——————————————-
    //Output: five

    • 0
  • Devang Singhal
    Dec, 2019 4

    public static string GetMaxOccuredWord(string strVar)
    {
    string maxOccuredWord = string.Empty;
    try
    {
    string[] strArray = strVar.Split(‘ ‘);
    var distinctArray = strArray.Distinct().ToArray();
    int[] maxCount = new int[distinctArray.Length];
    for (int index=0;index< distinctArray.Length;index++)
    {
    foreach (var strArrVal in strArray)
    {
    if(distinctArray[index]==strArrVal)
    {
    maxCount[index]=maxCount[index]+1;
    }
    }
    }
    int indexVar = Array.IndexOf(maxCount, maxCount.Max());
    maxOccuredWord = distinctArray[indexVar];
    }
    catch (Exception ex)
    {
    return ex.ToString();
    }
    return maxOccuredWord;
    }

    • 0
  • Niharika Basrani
    Jun, 2019 10

    String s1 = Console.ReadLine();Dictionary occurence = new Dictionary();string[] s1Array = s1.Split(' ');for(int i=0;i x.Value).FirstOrDefault().Key;Console.WriteLine(maxOcurred);Console.ReadLine();

    • 0
  • Maneesh Nishad
    Mar, 2019 15

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace logic_test {class Program{static void Main(string[] args){Program obj = new Program();Console.WriteLine(obj.most());Console.Read();}//str = "one two five one four five one two five two five two five" public string most(){string str = "one two five one four five one two five two five two five two two two";string[] ayy = str.Split(' ');int[] position=new int[ayy.Length];for(int i=0;i

    • 0
  • Ashutosh Awasthi
    Jul, 2018 4

    string str = "one two five one four five one two five two five two five";string[] d = str.Split(' ');Dictionary occurence = new Dictionary();foreach (var item in d){if (!occurence.ContainsKey(item))occurence.Add(item, 0);elseoccurence[item] += 1;}int maxElement=0;string key = String.Empty;foreach (KeyValuePair item in occurence){if (item.Value > maxElement){maxElement = item.Value;key = item.Key;}}Console.WriteLine(string.Format("Most occured key is : {0} and Occurence is : {1}", key, maxElement));

    • 0
  • naveen sharma
    Jul, 2017 4

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

    • 0
  • naveen sharma
    Jul, 2017 4

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

    • 0
  • naveen sharma
    Jul, 2017 4

    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;

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS