// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; class MainClass { public static Object MaxOccur(ArrayList list) { Hashtable ht = new Hashtable(); for(int i = 0; i < list.Count; i++) { Object o = list[i]; if(ht.ContainsKey(o)) { int n = (int)ht[o]; ht.Remove(o); ht.Add(o, n + 1); } else { ht.Add(o, 1); } } int maxn = 0; Object maxo = null; IDictionaryEnumerator en = ht.GetEnumerator(); while(en.MoveNext()) { if((int)en.Value > maxn) { maxn = (int)en.Value; maxo = en.Key; } } return maxo; } public static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add("A"); list.Add("BB"); list.Add("CCC"); list.Add("BB"); list.Add("A"); list.Add("A"); list.Add("BB"); list.Add("BB"); Object o = MaxOccur(list); Console.WriteLine(o); } }