Several Methods to remove repeated items in the list set

Source: Internet
Author: User

Several methods have been tried to remove duplicate data because list is used. Recorded here...

Test data:

List < String > Li1 = New List < String > { "  8  " , "  8  " , "  9  " , "  9 " , "  0  " , "  9  "  }; List < String > Li2 = New List < String > { "  Zhang San  " , " Zhang San  " , "  Li Si  " , "  Zhang San  " , "  Wang Wu  " , "  Li Si  " }; 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 1:

 
Hashset <String> HS =NewHashset <String> (Li1 );//The duplicate data has been removed and saved in the hashset.

 

Method 2:

For(IntI =0; I <li2.count; I ++)//The number of external Loops{For(IntJ = li2.count-1; J> I; j --)//The number of times an internal loop is compared with an external loop.{If(Li2 [I] =Li2 [J]) {li2.removeat (j );}}}

 

Method 3:

 // 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 4:

 //  This method is the same as above, but it just changes the logic.              For ( Int I = 0 ; I <li4.count- 1 ; I ++ ){ For ( Int J = 0 ; J <li4.count; j ++ ){  If (I! = J ){  If (LI4 [I] = LI4 [J]) {LI4 [J] = "  Null  "  ;}}}} 

 

Final output

Console. writeline ( "  The value after Li1 deduplication is  "  ); HS. tolist (). foreach (item => Console. writeline (item); console. writeline (  "  The value after li2 deduplication is  "  ); Li2.foreach (item => Console. writeline (item); console. writeline (  "  The value after li3 deduplication is "  ); Li3.foreach (item => Console. writeline (item); console. writeline (  "  The value after LI4 deduplication is  "  ); Li4.foreach (item => Console. writeline (item ));

 

 

Null is not removed. Remove it when using it.

 

Of course. There are many other methods. For example, you can use either of the following functions: remove the duplicate title content in modellist, Which is 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 , Title = "  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 1:The premise for comparison here is the object hashCodeEqual. Otherwise, it will not be compared because the hash code is not equal. The two objects are obviously not equal.

//Define a class that inherits the iequalitycomparer InterfacePublic ClassModelcomparer: iequalitycomparer <model>{Public BoolEquals (Model X, model Y ){ReturnX. Title. toupper () =Y. Title. toupper ();}Public IntGethashcode (Model OBJ ){ReturnOBJ. Title. toupper (). gethashcode ();}}

Call:

Modellist = modellist. Distinct (NewModelcomparer (). tolist ();

 

Solution 2:

 
VaRTitle = 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 ();Foreach(VaRItemInModellist) {console. writeline (item. ID+"\ T"+Item. Title );}

 

Of course. If you only compare two values for equality.

List <String> Li1 =NewList <String> {"8","8","9","8","0","9"}; Li1= Li1.distinct (). tolist ();

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.