List<string> list = new List<string>(); var c = (from li in list select li).Distinct();
public List<int> GetLists() { List<int> array = new List<int>(); array.Add(1); array.Add(2); array.Add(3); array.Add(4); array.Add(1); array.Add(3); List<int> list = new List<int>(); foreach (int i in array) { if (!list.Contains(i)) { list.Add(i); } } return list; }
List<string> list = new List<string>(); list.Add("aa"); list.Add("bb"); list.Add("bb"); list.Add("cc1"); list.Add("bb"); list.Add("cc1"); for (int i = 0; i < list.Count;) { if (list.LastIndexOf(list[i]) == i) { i++; } else { list.RemoveAt(i); } }
public static void Distinct(string arg) { string[] nums = arg.Split(','); if(nums.Length == 0) return; ArrayList result = new ArrayList(); for(int i = 0; i < nums.Length; i++) { if(result.Contains(Convert.ToInt32(nums[i])))continue; result.Add(Convert.ToInt32(nums[i]));//执行转换是为了实现数字排序 } result.Sort(); for(int i = 0; i < result.Count; i++)//结果 { Console.WriteLine(result[i].ToString()); } }
static void remove(List<string> strlist) { List<string> rs = new List<string>(); List<int> index = new List<int>(); int len = strlist.Count; for (int i = 0; i < len - 1; i++) { int j = i + 1; while (j < len) { if (strlist[i] == strlist[j]) { index.Add(j); strlist.RemoveAt(j); len = len - 1; } j++; } } }
重发下: static void remove(List<string> strlist) { List<string> rs = new List<string>(); int len = strlist.Count; for (int i = 0; i < len - 1; i++) { int j = i + 1; while (j < len) { if (strlist[i] == strlist[j]) { strlist.RemoveAt(j); len = len - 1; } j++; } } }
我晕,在去掉这个吧“List<string> rs = new List<string>();“
List<string> list = new List<string>(); var c = (from li in list select li).Distinct();
不知道这对楼主有没有用:string[] str = new string[] { "abc", "ab", "abc", "ad" }; List<string> list = new List<string>(); foreach (string s in str) { if (!list.Contains(s)) { list.Add(s); } } string[] str2 = list.ToArray(); Console.WriteLine(String.Join(", ", str2));输出结果:abc,ab,ad
List<string> list= GetList();//get data
List<string> resultList=list.Find(PredicateItems);//PredicateItems是个委托 private bool PredicateItems(item targetItem)
{
if (targetItem.itemID == _itemID)
{
return true;
}
else
{
return false;
}
}
List<int> array = new List<int>();array.Add(1);array.Add(2);array.Add(3);array.Add(1);
array.Add(2);for (int i = array.Count - 1; i >= 0; --i) {if (array.IndexOf(array[i]) != i) array.RemoveAt(i);
}string类似
var c = (from li in list
select li).Distinct();
{
List<int> array = new List<int>();
array.Add(1);
array.Add(2);
array.Add(3);
array.Add(4);
array.Add(1);
array.Add(3);
List<int> list = new List<int>();
foreach (int i in array)
{
if (!list.Contains(i))
{
list.Add(i);
}
}
return list;
}
List<string> list = new List<string>();
list.Add("aa");
list.Add("bb");
list.Add("bb");
list.Add("cc1");
list.Add("bb");
list.Add("cc1");
for (int i = 0; i < list.Count;)
{
if (list.LastIndexOf(list[i]) == i)
{
i++;
}
else
{
list.RemoveAt(i);
}
}
{
string[] nums = arg.Split(',');
if(nums.Length == 0) return;
ArrayList result = new ArrayList();
for(int i = 0; i < nums.Length; i++)
{
if(result.Contains(Convert.ToInt32(nums[i])))continue;
result.Add(Convert.ToInt32(nums[i]));//执行转换是为了实现数字排序
}
result.Sort();
for(int i = 0; i < result.Count; i++)//结果
{
Console.WriteLine(result[i].ToString());
}
}
Test:
Distinct("1,1,1,2,2,3,4,5,6,6,7");
Distinct("1,1,1,1,15,1,1,5,2,6,3");
Distinct("10,9,8,7,6,4,0,0,0,1,2");
各位朋友为什么还是不看标题呢
整个linq上来。
static void remove(List<string> strlist)
{
List<string> rs = new List<string>();
List<int> index = new List<int>();
int len = strlist.Count;
for (int i = 0; i < len - 1; i++)
{
int j = i + 1;
while (j < len)
{
if (strlist[i] == strlist[j])
{
index.Add(j);
strlist.RemoveAt(j);
len = len - 1;
}
j++;
}
}
}
static void remove(List<string> strlist)
{
List<string> rs = new List<string>();
int len = strlist.Count;
for (int i = 0; i < len - 1; i++)
{
int j = i + 1;
while (j < len)
{
if (strlist[i] == strlist[j])
{
strlist.RemoveAt(j);
len = len - 1;
}
j++;
}
}
}
List<string> list = new List<string>();
var c = (from li in list
select li).Distinct();
{
if (!list.Contains(s))
{
list.Add(s);
}
} string[] str2 = list.ToArray(); Console.WriteLine(String.Join(", ", str2));输出结果:abc,ab,ad