Because the list is used, there are several ways to remove duplicate data. Recorded in this ...
test Data:
list<string> li1 = new List<string> {"8", "8", "9", "9", "0", "9"};
list<string> Li2 = new List<string> {"John", "John", "Dick", "John", "Harry", "Dick"};
list<string> Li3 = new List<string> {"A", "a", "C", "A", "C", "D"};
list<string> Li4 = new List<string> {"12", "18", "19", "19", "10", "19"};
method One:
hashset<string> hs = new hashset<string> (LI1); At this point, the duplicate data has been removed and stored in HashSet.
Method Two:
for (int i = 0; i < Li2. Count; i++ //Outer loop is the number of loops
{for
(int j = li2). Count-1; J > i; j--) //Inner loop is the number of times the outer loop is compared
{
if (li2[i] = = Li2[j])
{
li2. RemoveAt (j);}}
Method Three:
Replace the same with null. for
(int i = 0; i < Li3. Count; i++)
{for
(int j = 0; J < Li3. Count; J + +)
{
if (i = = j) Continue;
if (li3[i] = = Li3[j])
{
li3[j] = "null"
;
}
}}
method Four:
This method is the same as above, except that the logic for
(int i = 0; i < LI4) is changed. Count-1; i++)
{for
(int j = 0; J < Li4. Count; J + +)
{
if (i!= j)
{
if (li4[i] = = Li4[j])
{
li4[j] = "null";
}
}}}
final output See results
Console.WriteLine ("Li1 Remove repetition value as");
Hs. ToList (). ForEach (Item => Console.WriteLine (item));
Console.WriteLine ("Li2 Remove repetition value as");
Li2. ForEach (Item => Console.WriteLine (item));
Console.WriteLine ("Li3 Remove repetition value as");
Li3. ForEach (Item => Console.WriteLine (item));
Console.WriteLine ("Li4 Remove repetition value as");
Li4. ForEach (Item => Console.WriteLine (item));
Null I did not remove. When you use it, you can remove it.
Of course. There are many other ways. Like LINQ Distinct and so on, look at this example on the Web: Remove the duplicate contents of the title in Modellist, case-insensitive
Class program
{
static void Main (string[] args)
{
list<model> modellist = new List<model> (
{New Model () {id = 1, title = "Abcde"},
New Model () {id = 2, title = "Abcde"},
New Model () {id = 3, tit Le = "AbcdE"},
New Model () {id = 4, title = "A"},
New Model () {id = 5, title = "A"}
};
Console.read ();
}
public class Model
{public
int ID {get; set;}
public string Title {get; set;}
}
Solution One: The premise of this comparison is that the hash code of the object is equal. Otherwise, it will not be compared because the hash code is not equal. Two objects are obviously not equal
Define a class inherits IEqualityComparer interface public
class modelcomparer:iequalitycomparer<model>
{public
bool Equals (Model X, model Y)
{return
x.title.toupper () = = Y.title.toupper ();
}
public int GetHashCode (Model obj)
{return
obj. Title.toupper (). GetHashCode ();
}
Call:
Modellist = modellist.distinct (New Modelcomparer ()). ToList ();
Solution Two:
var title = Modellist.groupby (M => m.title.tolower (). Trim ()). Select (M => new {id = M.firstordefault (). ID});
Modellist = Modellist.where (M => title. Select (Mo => mo.id). Contains (m.id)). ToList ();