62,242
社区成员




static List<int> GetSameItem3(int[] arrA, int[] arrB)
{
Dictionary<int, bool> dic = new Dictionary<int, bool>();
List<int> list = new List<int>();
foreach (int i in arrA)
{
if (!dic.ContainsKey(i))
dic.Add(i, false);
}
foreach (int i in arrB)
{
if (dic.ContainsKey(i))
{
dic[i] = true;
list.Add(i);
}
else
dic.Add(i, false);
}
return list;
}
static void Main(string[] args)
{
int[] arrA = new int[short.MaxValue];
int[] arrB = new int[short.MaxValue];
Random r = new Random(Environment.TickCount);
for (int i = 0; i < short.MaxValue; i++)
{
arrA[i] = r.Next(0, int.MaxValue);
arrB[i] = r.Next(0, int.MaxValue);
}
//以上是构建数据
int tick = Environment.TickCount;
GetSameItem1(arrA, arrB);
Console.WriteLine(Environment.TickCount - tick);
tick = Environment.TickCount;
GetSameItem2(arrA, arrB);
Console.WriteLine(Environment.TickCount - tick);
tick = Environment.TickCount;
GetSameItem3(arrA, arrB);
Console.WriteLine(Environment.TickCount - tick);
tick = Environment.TickCount;
GetSameItem4(arrA, arrB);
Console.WriteLine(Environment.TickCount - tick);
}
static List<int> GetSameItem1(int[] arrA, int[] arrB)
{
List<int> list = new List<int>();
foreach (int i in arrA)
if (Array.IndexOf(arrB, i) > -1)
list.Add(i);
return list;
}
static List<int> GetSameItem2(int[] arrA, int[] arrB)
{
List<int> list = new List<int>(arrA);
foreach (int i in arrB)
list.Remove(i);
return list;
}
static List<int> GetSameItem3(int[] arrA, int[] arrB)
{
Dictionary<int, bool> dic = new Dictionary<int, bool>();
List<int> list = new List<int>();
foreach (int i in arrA)
{
if (dic.ContainsKey(i))
{
dic[i] = true;
list.Add(i);
}
else
dic.Add(i, false);
}
foreach (int i in arrB)
{
if (dic.ContainsKey(i))
{
dic[i] = true;
list.Add(i);
}
else
dic.Add(i, false);
}
return list;
}
static List<int> GetSameItem4(int[] arrA, int[] arrB)
{
return arrA.Intersect(arrB).ToList();
}
string[] arry1 = new string[]{ "aa", "12", "bb" };
string[] arry2 = new string[]{ "bb", "16", "ll", "ds" };
foreach (string str in arry1)
{
if (Array.IndexOf(arry2, str) > -1)
{
MessageBox.Show(str.ToString());
}
}
int[] a = new int[10000];
int[] b = new int[10000];
bool[] flag = new bool[MAX];
for (int i = 0; i < MAX; i++)
flag[i] = false;
for (int i = 0; i < 10000; i++)
flag[a[i]] = true;
for (int i = 0; i < 10000; i++)
if (flag[b[i]])
Console.WriteLine(b[i]); //输出相同元素