score:7
string[] s = found.Split(',').Distinct().ToArray()
score:4
Rewrite the code that builds the result to output it directly.
ie. rewrite this:
for (m = r.Match(site); m.Success; m = m.NextMatch())
{
found = found + "," + m.Value.Replace(",", "");
}
return found;
To this:
return (from Match m in r.Matches(site)
select m.Value.Replace(",", "")).Distinct().ToArray();
This will return an array. If you still want it back as a string:
return string.Join(", ", (from Match m in r.Matches(site)
select m.Value.Replace(",", "")).Distinct().ToArray());
You may or may not be able to remove the last .ToArray()
from the last code there depending on the .NET runtime version. .NET 4.0 string.Join(...)
can take an IEnumerable<string>
, whereas previous versions requires an array.
score:4
This will return a string of comma seperated values without duplicates:
var result = string.Join(",",
r.Matches(site)
.Cast<Match>()
.Select(m => m.Value.Replace(",", string.Empty))
.Distinct()
);
score:4
this could be one possible solution:
var data = new List<string>();
for (m = r.Match(site); m.Success; m = m.NextMatch())
data.Add(m.Value.Replace(",", ""));
return String.Join(",", data.Distinct().ToArray());
score:0
You can achieve this in a single LINQ query
string strSentence = "aaa,bbb,ccc,aaa,111,111,ccc";
List<string> results = (from w in strSentence.Split(',') select w).Distinct().ToList();
Source: stackoverflow.com
Related Articles
- Removing duplicates from Array with C#.NET 4.0 LINQ?
- LINQ and removing duplicates from an array of objects
- removing duplicates in a list with linq
- Removing characters from strings with LINQ
- Select Distinct List of Words from Array with LINQ
- Is possible with LINQ to extract from an array of objects the value of one property and create a new array?
- How to remove duplicates from an Array using LINQ
- creating Linq to sqlite dbml from DbLinq source code
- LINQ for removing elements that are started with other element from list
- Removing strings with duplicate letters from string array
- Removing specific values from a Dictionary C# with Linq
- Removing from a collection while in a foreach with linq
- Can't add a new record with an integer value into database by using linq from code C#
- Get GameObject from array with specific name with LINQ
- Listing words from array with must letters using linq
- Using linq to merge multiple XML files with the same structure and removing duplicates based on a key
- Converting from Linq byte[] Concat to older .net Array with copy and Length considerations
- Linq code to get the index of an object in an array from an object within a list
- How to get grouped array of fields from DataTable with LINQ
- Find index from an array using regex with linq in c# .net
- linq select from array name with value
- c# finding items from a list with linq to an array
- Trouble with LINQ Returning Results Set from Object[] Array Within Object
- LINQ Query to find string of multidimensional array with most duplicates
- Removing Duplicates from LINQ results
- How I get the Attributevalue from a Xml to a Array with Linq to XML?
- How to search with LINQ entites having string[] array properties and find these containing any of string from string[] array?
- How to get missing value from two array in linq considering duplicates
- c# Linq or code to extract groups from a single list of source data
- Find a certain value from a source collection in a target collection of different type with linq
- Linq Nullable object must have a value. errors in .NET 6 and EF Core
- Linq to XML: I am not able to compare the nested element
- Debugging a C# Object Initializer
- C# console input
- Interesting behavior : LINQ query with integer
- Correctly Translate .Take(1)(0) to C# to fix Cannot apply indexing with [] error?
- Creating a one to many relationship
- Assigning a lambda expression causes it to not be executed later?
- C# get XElement attributes from XDocument with treeview path
- Membership provider login does not use indexes because of a lower()
- DataGridView Filtering OnClick Event (C# WinForm)
- Linq query takes too long when using Func & OrderByDescending
- Updating List using LINQ working when execute from Immediate window, not from code direct
- C# Complex Linq- how to get objects whose ids or children's ids match
- Lazy combinations
- Linq group by ID and then sum the ID itself (key)
- Unable to cast object of type System.Func`2 with Int64
- How to merge two lists that share 1 attribute into a third one with LINQ?
- Count objects within dynamic anonymous object (C#)
- XML to dropdown list using LINQ