score:1
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();
Source: stackoverflow.com
Related Query
- how to remove empty strings from list, then remove duplicate values from a list
- Remove non-constant duplicate words from strings in an array\list
- Select Distinct List of Words from Array with LINQ
- Generating the Shortest Regex Dynamically from a source List of Strings
- remove duplicate items from list in c#
- Remove Duplicate item from list based on condition
- Remove objects with duplicate properties from List
- Remove empty entries from list of list of strings
- How to remove substring from all strings in a list in C# using LINQ
- Removing strings with duplicate letters from string array
- Remove Back to Back Duplicate Entries from a List using Linq Method Syntax
- How to remove consecutive duplicate items from a list
- How to remove elements of a list where its string contains sub strings from another list
- Remove From Duplicate Starting Names From List Linq
- Remove items from one list if they contain strings from another list
- Remove duplicate objects with lowest Num property from list
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- How do I remove objects with a combination of duplicate of two properties from List with linq?
- Linq code to get the index of an object in an array from an object within a list
- remove duplicate values from a list with multiple properties
- Remove duplicate strings in list using C# / LinQ but ignore case
- How to get a list of non duplicate values from one property
- Remove substring from a list of strings
- Remove both duplicate items (if one is duplicate) from a list using Linq
- List from aggregated duplicate objects where a string property concats the strings from the duplicate objects in C#
- List or Array of String Contain specific word in Html Source Code
- Check if list of string contains object property value, and if it does remove it from the list of strings
- Remove duplicate objects from a list using LINQ in c#
- How to remove duplicate users from a list but amalgamate their roles
- Using C# 4.8: Most efficient way to remove items matching string patterns from a string array or string list
More Query from same tag
- FluentMongo LINQ: How to query a sublist of a class
- How to Expression.Invoke an arbitrary LINQ to SQL Query
- Cannot implicitly convert type 'bool?' to 'bool' in a LinQ query?
- EF Core 3.1 Intermediate Projection VS Iterating Result to Set Additional Property
- How to create where clause only by Expression
- Unit testing with Entity Framework
- Join multiple entities, 2 DbSet with one List in Entity framework
- Linq - find element in nested collections
- List mapping manually
- SQL to LINQ with outer joins and count
- Check property exists to stop error; cannot access child value on Newtonsoft.Json.Linq.JProperty
- Where all List values are equal to column List
- How to get all of relational records using LINQ
- Convert SQL query to LINQ
- C# LINQ to SQL - one to many relationship
- Case insensitive [Linq.Enumerable]::SequenceEqual()
- Copying data from var to DataTable after LINQ query
- c# / Linq sum where
- Issue in Linq to object where clause : the condition is bypassed
- Find duplicate sub-items from a List(of ParentObject) using LINQ
- Linq to Sql Count Include joins
- get data from DataTable using LINQ
- Get lambda expression from where clause or IQueryable/IEnumerable
- A Linq ContainsAll Method?
- processing Xnode with Linq efficiently
- An error while doing a join linq query
- search database table record using two parameters in linq
- I want to group list of list
- LINQ : prevent creating of new object, ( appending to a list attribute if object exists )
- LinQ Remove products based on Status