public List <ItemCount> analyze(List <String> list) { list.Sort(); List <ItemCount> icList = new List <ItemCount>(); String prev = null; ItemCount counter = null; foreach (var s in list) { if (s.Equals(prev)) { // Same item. Just increment the count counter.increment(); } else { // Item changed. Add previous item count to icList if (counter != null) { icList.Add(counter); } counter = new ItemCount(s, 1); prev = s; } } // The last item has not yet been added to the list if (counter != null) { icList.Add(counter); } return(icList); }
public Dictionary <string, ItemCount> analyzeDict(List <String> list) { list.Sort(); Dictionary <string, ItemCount> icList = new Dictionary <string, ItemCount>(); String prev = null; ItemCount counter = null; foreach (var s in list) { if (s.Equals(prev)) { // Same item. Just increment the count counter.increment(); } else { // Item changed. Add previous item count to icList if (counter != null) { icList.Add(s + "_" + counter.getCount().ToString(), counter); } counter = new ItemCount(s, 1); prev = s; } } // The last item has not yet been added to the list if (counter != null) { icList.Add(counter.getItem() + "_" + counter.getCount().ToString(), counter); } return(icList); }