Answers for "c# remove duplicates from list"

C#
1

c# list remove duplicate items

List<string> lstWhitDuplicate = new list<string>();
lstWhitDuplicate.add("hallo");
lstWhitDuplicate.add("hallo");
lstWhitDuplicate.add("World");

List<string> lst = lstWhitDuplicate.Distinct().ToList(); 

// output lstWhitDuplicate:
// hallo hallo world
// output lst:
// hallo word
Posted by: Guest on August-14-2021
1

remove duplicated from object array c#

import System.Linq;
// for simple array
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2 };
int[] dist = nums.Distinct().ToArray();

// for object array/list
// example class to remove duplicates based on property
class Order{
	public int i, j;
  	Order(int i, int j){
    	this.i = i;
     	this.j = j;
    }
}

// create this class
class Comparer : IEqualityComparer<Connection> {
    public bool Equals(Order x, Order y) {
        return x.i == y.i; // based on variable i
    }
    public int GetHashCode(Order obj) {
        return obj.i.GetHashCode(); // hashcode of variable to compare
    }
}

// main
List<Order> list = new List<Order>();
list.Add(1, 2);
list.Add(2, 3);
list.Add(1, 4);
list.Add(4, 5);
list.Add(1, 6);
// duplicates removed
List<Order> rem_dup = list.Distinct(new Comparer()).ToList();
// List rem_dup has no duplicates based on property i of Order class
Posted by: Guest on October-11-2020
0

c# list remove item based on property duplicate

fooArray.GroupBy(x => x.Id).Select(x => x.First());
Posted by: Guest on May-09-2020
0

c# remove duplicates from list

List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
Posted by: Guest on November-04-2021

Code answers related to "c# remove duplicates from list"

C# Answers by Framework

Browse Popular Code Answers by Language