score:3
var list = new list<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
list<int> result = list.where((x, index) =>
{
return index == 0 || x != list.elementat(index - 1) ? true : false;
}).tolist();
this returns what you want. hope it helped.
score:1
did you try distinct
?
var list = new [] { 10, 20, 20, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
list = list.distinct();
edit: since you apparently only want to group items with the same values when consecutive, you could use the following:
var list = new[] { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
list<int> result = new list<int>();
foreach (int item in list)
if (result.any() == false || result.last() != item)
result.add(item);
score:1
you can use contains and preserve order
list<int> newlist = new list<int>();
foreach (int n in numbers)
if (newlist.count == 0 || newlist.last() != n)
newlist.add(n);
var newarray = newlist.toarray();
output:
10, 1, 5, 25, 45, 40, 100, 1, 2, 3
score:1
it may be technically possible (though i don't think you can with a one-liner) to solve this with linq, but i think it's more elegant to write it yourself.
public static class extensionmethods
{
public static ienumerable<t> packgroups<t>(this ienumerable<t> e)
{
t lastitem = default(t);
bool first = true;
foreach(t item in e)
{
if (!first && equalitycomparer<t>.default.equals(item, lastitem))
continue;
first = false;
yield return item;
lastitem = item;
}
}
}
you can use it like this:
int[] packed = myarray.packgroups().toarray();
it's unclear from the question what should be returned in the case of 1,1,2,3,3,1
. most answers given return 1,2,3
, whereas mine returns 1,2,3,1
.
score:3
var list = new list<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
var result = list.where((item, index) => index == 0 || list[index - 1] != item);
Source: stackoverflow.com
Related Query
- How to group the same values in a sequence with LINQ?
- How to group only subsequent items with the same property using linq
- How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum total
- C# how to group List by objects with the same property values
- How to remove two objects with the same ID but different date values with LINQ to objects
- How do I get the MAX row with a GROUP BY in LINQ query?
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- Linq OrderBy to group objects with the same sets
- C# LINQ Select objects with the same value of one property join values of other
- How can I group by the difference between rows in a column with linq and c#?
- How to sort linq with fixed values and show all the rest with another sorting
- How to swap the data source associated with a Linq query?
- LINQ group by with join and count occurence of the same item
- How do I use LINQ to parse XML with child elements of the same name?
- How to find consecutive same values items as a Linq group
- c# linq how to check for null values in the same column that i need to be not null
- How to calculate STD dev and Average with linq and entity framework at the same time
- How to get different values with same name from xml with linq
- How to concatenate all child elements with same names values using LINQ to XML
- Group in Linq with Max: How do I get the full data set of the max value?
- Linq to XML C# Descendants: How to read Attributes with the same name?
- How to query XML with the same element and attribute name using linq
- How to query values associated with the foreign key using linq and display in listview
- How do I select with linq an attribute from an xml tag on the same level?
- In a many to many relationship , how to retrieve the values like 2-D array with help of linq
- How to make a simple formula with the current attributes in LINQ Group By?
- How to sort a list with the values of another list with Linq
- linq group by two columns and get only rows with same group by values
- How to creat a new list from existing list with elements which contains the same id in Linq to object?
- How to retrieve multiple column values with same foreign key id using Linq method Syntax in C#?
More Query from same tag
- Cannot use ternary operator in LINQ query
- Populate class from an XML using Linq
- Using linq query to receive a modified List<tuple<>>
- C# Serialize data with linq for chartist.js
- Order by, distinct and select top 5 results with linq
- How to Get All records with specific Id and LastDate
- Linq Dynamic Query With Group By
- Is Queryable.OrderBy unstable for SQL Server database?
- How to change the content of elements and attributes
- Get MAX 5 items from a grouped List with LINQ and C#
- Is there a way to get all the items from a list that have a specific property
- LINQ with AND or OR for 2 or more enumerable lists
- Finding records where a field value not in a list
- Using LINQ to determine if a value exists in a DB table
- LINQ distinct on class item?
- Find overlapping dates in 2 lists and return overlapping period
- method to process linq group
- LINQ + WCF + Transactions
- Compile error when calling ToList() when accessing many to many with Linq To Entities
- How do you enumerate child properties recursively with an entity tree structure?
- Dynamic Linq Select
- Using Linq count function with vb.net datatable
- How to make LINQ to SQL backward compatible
- Painless way to dispose injected LINQ data context?
- LINQ query returning objects with property value of specific subtype
- Object.CategoryName into List<string> of category names
- C# how do I implement inheritance using linq?
- LINQ using group by and ascending order
- How to check name of property in KeySelector?
- How to search data from Database using Linq