public IList <string> JoinAdjacentWords(IList <string> wordList, IList <string> keywordsList) { if (wordList.Count == keywordsList.Count) { return(null); } var modifiedPhrases = new HashSet <string>(); var dealtWith = new HashSet <string>(); string firstWord = null, secondWord = null; for (int i = 0, j = 1; j < wordList.Count; i++, j++) { firstWord = wordList[i]; secondWord = wordList[j]; if (keywordsList.Contains(firstWord) && keywordsList.Contains(secondWord)) { modifiedPhrases.Add($"{firstWord} {secondWord}"); dealtWith.AddMultipleElements <string>(firstWord, secondWord); } else { if (keywordsList.Contains(firstWord) && !dealtWith.Contains(firstWord)) { modifiedPhrases.Add(firstWord); } //Last Word condition if (j == wordList.Count - 1 && keywordsList.Contains(secondWord) && !dealtWith.Contains(secondWord)) { modifiedPhrases.Add(secondWord); } } } return(modifiedPhrases.ToList()); }