Reputation: 331
So i made code that finds longest words in two text files and if that is unique word in first text file it writes to file. But i need to find unique words in first text file and then from those unique words find 10 longest words. Then those 10 words sort from longest to shortest and count how many times it appears in first text file.
string[] LongestWrods(string[] longest1, string[] text2, int longestCount1, out int longestWordText, char[] @char)
{
string[] LongestWordsText1 = new string[10];
longestWordText = 0;
for (int i = 0; i < longestCount1; i++)
{
if (RepeatTimes(text2, longest1[i], @char) == 0)
LongestWordsText1[longestWordText++] = longest1[i];
}
return LongestWordsText1;
}
Upvotes: 1
Views: 229
Reputation: 5201
This way:
class Program
{
static void Main(string[] args)
{
List<string> wordsToCut = File.ReadAllLines("text2.txt").Distinct().ToList();
List<UniqueWord> uniqueWords = new List<UniqueWord>();
foreach (string word in File.ReadAllLines("text1.txt"))
{
if (wordsToCut.Contains(word) == false)
{
UniqueWord uniqueWord = uniqueWords.Where(x => x.Word == word).FirstOrDefault();
if (uniqueWord != null)
{
uniqueWord.Occurrence++;
}
else
{
uniqueWords.Add(new UniqueWord(word));
}
}
}
uniqueWords = uniqueWords.OrderByDescending(x => x.Word.Length).Take(10).ToList();
}
}
public class UniqueWord
{
public string Word { get; set; }
public int Occurrence { get; set; }
public UniqueWord(string word)
{
Word = word;
Occurrence = 1;
}
}
Upvotes: 1
Reputation: 148
Of course not the best option, but fastest one i could get. largestWordsCount
contains all the 10 largest unique words and how times it appears in text for each.
var text = "The standard Lorem Ipsum passage, standard used standard since the 1500s \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed";
var splittedText = text.Split(' ');
var uniqueWords = new HashSet<string>(splittedText);
var tenLargestWords = uniqueWords.ToList().OrderByDescending(x => x.Length).Take(10);
var largestWordsCount = tenLargestWords.Select(word => new KeyValuePair<string, int>(word, Regex.Matches(text, word).Count)).ToList();
Upvotes: 0