Remove mutable duplicate words from strings in \ list array

I am trying to extract primary words from a large set of very long lines in order to make it easier to display so ...

Assuming we have a string array that outputs:

Something One
Something [ABC] Two
Something [ABC] Three
Something Four Section 1
Something Four Section 2
Something Five

How to remove duplicate words non-constant, such as Somethingand [ABC]so that it leaves only the unique identifiers of each line, for example One Two Three, and display this list:

One
Two
Three
Four Section 1
Four Section 2
Five

Knowing that:

  • Duplicate: any word repeated more than once in the list

  • { "", "", "",..}, , - , , { "" "", "Charlie" } { "Nu", "Xi", "Pi" } , .

  • ( ) " 1", "- 1" " 1"

+4
2

, ( ), , Section 1". . 2 .

1) FindRepeatedWords - , hashset UniqueWords Repeats hashset. , , - , .

2) CleanUpWordsAndDoNotChangeList - , , . .

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;
        }
    }
}
+1

, , .

:

        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();
0

Source: https://habr.com/ru/post/1677643/


All Articles