There is a line with random words written with a comma. Tell me how to determine the number of words with the same length.
|
2 answers
int length = 5; //длина слова char[] splitsigns = { ',' }; List<string> words = "With,just,one,day,left,in,the,presidential,race,Hillary,Clinton,holds,a,four-point,lead,over,Donald,Trump,nationally".Split(splitsigns).ToList(); int amount = words.Count(w => w.Length == length); - Ok, thanks, figured out - Ares
- 2@Ares and minus what for? I help you, and you give me a minus, you are a good person! - Bulson
- oneOnly in the question of grouping by length, and you count the number of words of one particular length. Something does not converge - Alexey Shimansky
- @ Alexey Shimansky, I will allow myself to quote the question: "There is a line with random words written with a comma. Tell me how to determine the number of words with the same length." And I'm sorry I did not read what was written in the comments, because At this time, wrote the answer. - Bulson
|
Solution through LINQ already offered ...
Therefore, I will offer a solution in the old-fashioned way:
Dictionary<int,int> resultDict=new Dictionary<int, int>();//Ключем словаря является длинна, а значением число повторений char[] splitsigns = { ',' }; List<string> words = "With,just,one,day,left,in,the,presidential,race,Hillary,Clinton,holds,a,four-point,lead,over,Donald,Trump,nationally".Split(splitsigns).ToList(); foreach (var word in words) { if (resultDict.ContainsKey(word.Length)) { resultDict[word.Length]++; } else { resultDict.Add(word.Length,1); } } |
s.Split(',').GroupBy(s => s.Length).Select(n => n.Count()).ToArray();True, in real conditions, the result is not very useful, but now you have something to push off from - eastwing