(1)去重
int[] arr = { 1, 4, 3, 1, 6, 5, 6 };
int[] arr2 = arr.Distinct().ToArray();
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine(arr2[i]);
}
Console.ReadKey();
(2)
//提取重复的
int[] arr = { 1, 4, 3, 1, 6, 5, 6 };
var arr3 = arr.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();
for (int i = 0; i < arr3.Count; i++)
{
Console.WriteLine(arr3[i]);
}
Console.ReadKey();
(3)
//找出重复的元素个数
var query = arr.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => new { 元素 = y.Key, 重复个数 = y.Count() }).ToList();
for (int i = 0; i < query.Count; i++)
{
Console.WriteLine(query[i]);
}
Console.ReadKey();
去重算法
static void Main(string[] args) {
int[] a = {1,5,3,5,1,8 };
int[] b=Del(a);
for (int i = 0; i < b.Length; i++){
Console.WriteLine(b[i]);
}
Console.ReadKey();
}
static int[] Del(int[] a) {
int len = 0;
int[]b=new int[a.Length];
for (int i = 0; i < a.Length; i++,len++){
b[len] = a[i];
for (int j = i+1; j < a.Length; j++){
if (a[i]==a[j]){
len--; break;
}
}
}
int[] new_a = new int[len];
for (int k = 0; k < len; k++) {
new_a[k] = b[k];
}
return new_a;
}