Here is the problem: https://www.codeeval.com/open_challenges/132/ And here is my solution. I get 100% with this code.
using System; using System.IO; using System.Linq; using System.Text; using System.Collections.Generic; class TheMajorElement { public static string line = null; static void Main(string[] args) { List<string> readFile = ReadFileLines(args[0]); string result = FindMajorElement(readFile); Console.WriteLine(result); } public static List<string> ReadFileLines(string fileName) { List<string> readFileLines = new List<string>(); StreamReader reader = new StreamReader(fileName); using (reader) { line = reader.ReadLine(); while (line != null) { readFileLines.Add(line); line = reader.ReadLine(); } } return readFileLines; } public static string FindMajorElement(List<string> elements) { List<int> numbers = new List<int>(); string checkResult = null; string result = null; int length = elements.Count; int numbersCount = 0; int count = 0; int number = 0; int limit = 0; char[] separator = new char[] { ' ', ',' }; for (int i = 0; i < length; i++) { line = elements[i]; numbers = line.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList(); numbersCount = numbers.Count; limit = numbersCount / 2; for (int j = 0; j < numbersCount; j++) { number = numbers[j]; count = numbers.Count(x => x == number); if(count > limit) { checkResult += number + "\n"; break; } } if (checkResult == null) { result += "None\n"; } else { result += checkResult; } checkResult = null; } return result.TrimEnd('\n'); } }