score:1

Accepted answer

This solution assumes you know nothing (just like John Snow) except the certain words such as Section 1". It works for arbitrary string input. It has 2 main points.

1) FindRepeatedWords is a method which fills up UniqueWords hashset and Repeats hashset. UniqueWords, as the name suggests is the everyunique word in the list and Repeats are repeated words.

2)CleanUpWordsAndDoNotChangeList is the main method which does what you want. It decides to remove the words based on certain words.

namespace StackOverfFLow {

    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class Program {
        private static readonly HashSet<string> UniqueWords = new HashSet<string>();
        private static readonly HashSet<string> Repeats = new HashSet<string>();
        private static readonly List<string> CertainWords = new List<string> { "Section 1", "Section 2" };
        private static readonly List<string> Words = new List<string> { "Something One", "Something [ABC] Two", "Something [ABC] Three", "Something Four Section 1", "Something Four Section 2", "Something Five" };

        private static void Main(string[] args) {
            FindRepeatedWords();
            var result = CleanUpWordsAndDoNotChangeList();
            result.ForEach(Console.WriteLine);
            Console.ReadKey();
        }

        /// <summary>
        /// Cleans Up Words And Des oNot Change List.
        /// </summary>
        /// <returns></returns>
        private static List<string> CleanUpWordsAndDoNotChangeList() {
            var newList = new List<string>();
            foreach(var t in Words) {
                var sp = SeperateStringByString(t);
                for(var index = 0; index < sp.Count; index++) {
                    if(Repeats.Contains(sp[index]) != true) { continue; }
                    var fixedTocheck = sp.ElementAtOrDefault(index + 1);
                    if(fixedTocheck == null || CertainWords.Contains(fixedTocheck)) { continue; }
                    sp.RemoveAt(index);
                    index = index - 1;
                }
                newList.Add(string.Join(" ", sp));
            }
            return newList;
        }

        /// <summary>
        /// Finds Unique and Repeated Words.
        /// </summary>
        private static void FindRepeatedWords() {
            foreach(var eachWord in Words) {
                foreach(var element in SeperateStringByString(eachWord)) {
                    if(UniqueWords.Add(element) == false) { Repeats.Add(element); };
                }
            }
        }

        /// <summary>
        /// Seperates a string by another string
        /// </summary>
        /// <param name="source">Source string</param>
        /// <returns></returns>
        private static List<string> SeperateStringByString(string source) {
            var seperatedStringByString = new List<string>();
            foreach(var certainWord in CertainWords) {
                var indexOf = source.IndexOf(certainWord);
                if(indexOf <= -1) { continue; }
                var a = source.Substring(0, indexOf).Trim().Split(' ');
                seperatedStringByString.AddRange(a);
                seperatedStringByString.Add(certainWord);
            }
            if(seperatedStringByString.Count < 1) { seperatedStringByString.AddRange(source.Split(' ')); }
            return seperatedStringByString;
        }
    }
}

score:0

I`m not sure is it this you want but I will past my code.

Fast code:

        string itemName = "";
        List<string> destinationArray = new List<string>();

        List<string> inputArrayList = new List<string>();
        inputArrayList.Add("Something One");
        inputArrayList.Add("Something [ABC] Two");
        inputArrayList.Add("Something [ABC] Three");
        inputArrayList.Add("Something Four Section 1");
        inputArrayList.Add("Something Four Section 2");
        inputArrayList.Add("Something Five");
        inputArrayList.Add("Other Text");

        List<string> allWordList = new List<string>();

        foreach (var item in inputArrayList)
        {
            allWordList.AddRange(item.Split(' ').ToList());
        }

        List<string> searchingArrayList = new List<string>();
        searchingArrayList = allWordList.GroupBy(x => x)
                    .Where(group => group.Count() > 1)
                    .Select(group => group.Key).ToList();

        foreach (var itemInput in inputArrayList)
        {
            itemName = itemInput;
            foreach (var itemSearching in searchingArrayList)
            {
                itemName = itemName.Replace(itemSearching, "");
            }
            destinationArray.Add(itemName);
        }

        destinationArray.ToList().ForEach(x => Console.WriteLine(x));
        Console.ReadKey();

Related Articles