c# - Removing duplicate entries from arralist -
i have arraylist "templist" after point of time saw lot of duplicate values added arraylist, thought of remove duplicates.when googled found solutions of them confusing.some telling sort arralist , find , remove duplicates telling use distinct, somewhere telling use hashlist. way dont want replace arraylist other structures. can provide me logic prevent duplicates arraylist?
the code having
public class templist : arraylist { //some logic behind arraylist } templist items = new templist(); if (!(obj testobject) && !(obj leafobject)) { items.add(obj); } if (items.count > 0) { ssendobjectcollection(items); } static public void sendobjectcollection(templist col) { try { col.sort(); } catch(system.exception exception) { } }
update
i did code
public static string[] removeduplicates(string[] s) { hashset<string> set = new hashset<string>(s); string[] result = new string[set.count]; set.copyto(result); return result; }
then called in exisitng function sendobjectcollection() removeduplicates(col);
throwing errors, can body guide me going wrong here?
there 2 problems have solve. firstly have remove duplicate values using:
myarray = new arraylist(myarray.distinct().toarray());
and checking duplicate before add value array list:
if(myarray.indexof("value") == -1) {myarray.add("value");}
hope help
Comments
Post a Comment