int[] a = new int[] { 10, 20, 10, 30, 40, 20, 50, 60 };
int[] c = new int[10];
int count = 0;
int chk = 0;
int temp;
bool b=false;
for (int i = 0; i < a.Length; i++)
{ chk = 0;
if (a[i] != 0)
{
for (int j = 0; j < a.Length; j++)
{
if (a[i] == a[j])
{
chk++;
}
}
}
if (chk == 1)
{
c[count] = a[i];
count++;
}
}
foreach (var x in c)
{
if(x!=0)
{
Response.Write(x);
}}
答案 0 :(得分:0)
您可以使用词典:
var dicArray = new Dictionary<int, int>();
将数组的所有值插入到Dictionary中,如:
foreach of array:
dicArray[a[i]] = a[i];
然后将dicArray的所有值都带到新数组(int)。
词典没有inser双键,这样键X只插入一次。
答案 1 :(得分:0)
使用LINQ。
int[] a = { 10, 20, 10, 30, 40, 20, 50, 60 };
int[] c = a.Distinct().ToArray();
答案 2 :(得分:0)
使用HashSet保留唯一值!
int[] a = new int[] { 10, 20, 10, 30, 40, 20, 50, 60 };
var b = new HashSet<int>(a); //HashSet of your values
var c = b.ToArray(); //Array from hashset