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