I. Preface
Deduplication is often encountered in our development process, avoiding the addition of duplicate elements, we need to get to the collection (including list, array, etc.)
do the relevant filtering operation. ensure the uniqueness of the image so that the data is not redundant and repeatable. Since you are developing the. NET aspect, take C #
in Language Common way of de-weight to learn. If there are other better ways, I hope you will point out that I also learn to learn, thank you very much!
two. C # General de-weight
Static voidMain (string[] args) { //1. Definition of a collectionlist<int> Listarr =Newlist<int> {1,2,3,5,8,Ten }; List<int> Listarrcompare =Newlist<int> {2,3,4,8, }; varNUM1 =""; varnum2 =""; varNUM3 =""; //2. Operation of the collectionListarr.addrange (Listarrcompare);//merging Listarr and ListarrcompareList<int> resultlist = listarr.union (listarrcompare). tolist<int> ();//Reject Duplicateslist<int> resultsamelist = Listarr.concat (listarrcompare). tolist<int> ();//Keep Duplicates foreach(varItem1inchListarr) {NUM1= Num1 +","+item1. ToString (); } foreach(varItem2inchresultlist) {num2= num2 +","+item2. ToString (); } foreach(varItem3inchresultsamelist) {num3= num3 +","+Item3. ToString (); } Console.WriteLine (NUM1); Console.WriteLine (num2); Console.WriteLine (NUM3); Console.readkey (); }
Results:
C # There are many ways to go to the heavy, in here to the most basic, for dictionary, HashTable, hashset and so can be used to the method of heavy.
The Add method of the hashset<t> returns the bool value, ignoring this operation and returning a false value if the data is added, if it is already present in the collection.
Hashtable and Dictionary<tkey,tvalue> will throw an error directly when they encounter repeated additions.
three. C # uses a for-loop traversal comparison to remove weight
1 //a set of inside a set to go heavy, using a circular comparison2 vartemp ="";3list<int> Lstall =Newlist<int> {1,2,3,4,5,5,8,8,Ten };4 for(inti =0; I < Lstall. Count-1; i++)5 {6 for(intj = Lstall. Count-1; J > i; j--)7 {8 if(Lstall[i] = =Lstall[j])9 {Ten Lstall. RemoveAt (j); One } A } - } - the foreach(varIteminchLstall) - { -Temp + = Item. ToString () +","; - } + Console.WriteLine (temp); -Console.readkey ();
Results:
four. Distinct to heavy
//using distinct to weighList<string> strsamelist =NewList<string>(); Strsamelist.add ("Wuhan"); Strsamelist.add ("Wuhan"); Strsamelist.add ("Shanghai"); Strsamelist.add ("Shanghai"); Strsamelist.add ("Nanjing"); Strsamelist.add ("Nanjing"); Strsamelist.add ("Royal Park"); Strsamelist.add ("Shijiazhuang"); String myList=String.Empty; IEnumerable<String> distinctlist =strsamelist.distinct (); foreach(String strinchdistinctlist) {MyList+ = str +" ,"; } Console.WriteLine (MyList); Console.readkey ();
Results:
Five. de-weight in JavaScript
In JavaScript we often encounter the situation of the array to heavy, then we generally take what way?
Through learning and some information on the Internet, I will summarize some methods as follows:
5.1 The first method:
By defining the method of the array object's prototype, the array can call the method on its own repeating element drop.
Because methods on the array prototype are shared for different array objects.
1array.prototype.droprepeat=function () {2 varOldarr= This;3 varoldlen=oldarr.length;4 varnewarr=[oldarr[0]];5 6 if(!Oldlen)7 {8 return This;9 }Ten One for(varI=1; i<oldlen;i++) A { -Newarr.indexof (Oldarr[i]) <0? Newarr.push (Oldarr[i]):"'; - } the returnNEWARR; - - }; - + vartestarr=['1','2','3','3','5']; -Console.log (Testarr.droprepeat ());
The results of the test under Chrome are:
5.2 The second method is the direct function implementation:
1 function Droprepeat (arr) {2 //traverse arr to place elements into the TMP array (not present)3 varTMP =NewArray ();4 for(varI=0; i<arr.length;i++){5 //The element does not exist inside TMP to allow append6 if(Tmp.indexof (Arr[i]) ==-1){7 Tmp.push (Arr[i]);8 }9 }Ten returntmp; One } A vartestarr=[1,2,2,3,4,4]; -Droprepeat (Testarr);
Results:
5.3 The third method is to use a key value method, after the reversal of the key value, because the key value can not be duplicated, so it automatically filtered out the duplicate elements.
1 function Droprepeat (arr)2 {3 varreversearr1=NewArray ();4 for(varItemincharr) {5reversearr1[arr[item]]=1;6 }7 varReversearr2=NewArray ();8 for(varIteminchreverseArr1) {9 Ten Reversearr2.push (item); One } A returnreverseArr2; - } - the vartestarr=[1,2,2,3,4,4,5]; -Droprepeat (Testarr);
Test results:
I rookie, I hope you Daniel give guidance, improper to accept learning! Thank you!
De-summarizing in C # and JavaScript