Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ValueCounter
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Record start time and the end
- DateTime startTime = DateTime.Now, overTime = DateTime.Now;
- LogRecoder("Create source file path");
- string sourcePath = "SourceTxt.txt";
- LogRecoder("Create dictionary");
- Dictionary<string, int> myDic = new Dictionary<string, int>();
- // Load value
- LogRecoder("Load value from source file");
- string data = System.IO.File.ReadAllText(sourcePath);
- foreach (string line in data.Split('\n'))
- {
- foreach (string value in line.Split(','))
- {
- if (myDic.ContainsKey(value))
- {
- myDic[value]++;
- }
- else
- {
- myDic.Add(value, 1);
- }
- }
- }
- // Search Max
- LogRecoder("Search max value and count");
- int maxValue = 0, maxValueCount = 0;
- foreach (KeyValuePair<string, int> item in myDic)
- {
- if (item.Value > maxValueCount)
- {
- maxValue = int.Parse(item.Key);
- maxValueCount = item.Value;
- }
- } overTime = DateTime.Now;
- // Output
- LogRecoder(string.Format("maxValue : {0}, maxValueCount : {1}", maxValue.ToString(), maxValueCount.ToString()));
- LogRecoder(string.Format("Process exited after {0} seconds", (Math.Round((overTime - startTime).TotalMilliseconds) / 1000).ToString("0.000")));
- LogRecoder("Press any key to continue ..");
- Console.ReadKey();
- }
- static bool LogRecoder(string context)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + context);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment