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
Select an image from your device to upload
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”)); }
public static string GetMaxWord(string word) { string[] wordlist = word.Split(' '); var diswords = wordlist.Length; double[] number = new double[diswords]; var j = 0; foreach (var item in wordlist) { for (int i = 0; i < diswords; i++) { if (item.ToLower() == wordlist[i].ToString().ToLower()) { number[j] = number[j] + 1; } } j++; } double k = 0; var l = -1; for (int i = 0; i < number.Length; i++) { if (k < number[i]) { k = number[i]; l = i; } } return wordlist[l]; }}
public static string GetMaxWord(string word)
{
string[] wordlist = word.Split(' ');
var diswords = wordlist.Length;
double[] number = new double[diswords];
var j = 0;
foreach (var item in wordlist)
for (int i = 0; i < diswords; i++)
if (item.ToLower() == wordlist[i].ToString().ToLower())
number[j] = number[j] + 1;
}
j++;
double k = 0;
var l = -1;
for (int i = 0; i < number.Length; i++)
if (k < number[i])
k = number[i];
l = i;
return wordlist[l];
//——————————————-//Output: five
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; }