两个数组:
string[] str1 = new string[] { "1", "2", "3", "4", "6", "9", "10" };
string[] str2 = new string[] { "1", "3", "4", "6", "8", "11" };
求两个数组的相同值
string[] str3 = new string[]{"1", "3", "4", "6"};
方法一:一步到位
string[] str1 = new string[] { "1", "2", "3", "4", "6", "9", "10" }; string[] str2 = new string[] { "1", "3", "4", "6", "8", "11" }; string[] str3 = str1.Where(t => str2.Contains(t)).ToArray();
方法二:以空间换取时间
1,扫描数组str1,将其值作为key,值为1。如dic.Add(key,1)
2,扫描数组str2,将其值作为key,与Dictionary的key比较,若key相等,设置dic的值为0
3,扫描Dictionary,若值为0,则key为str1与str2相等的值。
1 string[] str1 = new string[] { "1", "2", "3", "4", "6", "9", "10" }; 2 string[] str2 = new string[] { "1", "3", "4", "6", "8", "11" }; 3 4 //string[] str3 = str1.Where(t => str2.Contains(t)).ToArray(); 5 6 ArrayList array = new ArrayList(); 7 Dictionary<string, int> dic = new Dictionary<string, int>(); 8 for (int i = 0; i < str1.Length; i++) 9 { 10 dic.Add(str1[i], 1); 11 } 12 for (int i = 0; i < str2.Length; i++) 13 { 14 if (dic.ContainsKey(str2[i])) 15 { 16 dic[str2[i]] = 0; 17 } 18 } 19 foreach (string key in dic.Keys) 20 { 21 if (dic[key] == 0) 22 { 23 array.Add(key); 24 } 25 }
方法三,自定义数组
使用快速排序算法,将两个数组排序,然后进行比较
具体算法如下
1 public int[] Fun(int[] a,int[] b) 2 { 3 if(a.Length == 0 || b.Length == 0) 4 { 5 return null; 6 }//两个数组不为空 7 int aIndex = 0; 8 int bIndex = 0; 9 int cIndex = 0; 10 Sort(a, 0, a.Length - 1); 11 Sort(b, 0, b.Length - 1);//排序 12 int[] c = new int[a.Length > b.Length ? a.Length : b.Length];//定义一个新数组 13 for(; aIndex < a.Length; aIndex++) 14 {//遍历a数组 15 while(bIndex < b.Length && a[aIndex] >= b[bIndex])//查找b数组中的相同项 16 { 17 if(a[aIndex] == b[bIndex]) 18 {//有相等 19 c[cIndex] = a[aIndex];//插入数组c 20 bIndex++;//b索引下标加1 21 cIndex++;//c索引下标加1 22 break; 23 } 24 else if(a[aIndex] > b[aIndex]) 25 {//a>b 26 bIndex++;//b索引下标加1,继续while循环 27 } 28 else//b>a,退出循环,比较a的下一个 29 { 30 break; 31 } 32 33 } 34 } 35 for(int i = 0; i < cIndex; i++)//打印 36 { 37 Console.WriteLine(c[i]); 38 } 39 return c;//返回结果 40 } 41 42 public void Sort(int[] intArray, int nLower, int nUpper) 43 { 44 if(nLower < nUpper) 45 { 46 int nSplit = Partition(intArray, nLower, nUpper); 47 ///递归排序 48 Sort(intArray, nLower, nSplit - 1); 49 Sort(intArray, nSplit + 1, nUpper); 50 } 51 } 52 53 /// <summary> 54 /// 方法参数:原始数组、第一个元素位置、最后元素位置 55 /// 方法功能:完成一趟快速排序 56 /// </summary> 57 /// <param name="intArray"></param> 58 /// <param name="nLower"></param> 59 /// <param name="nUpper"></param> 60 /// <returns></returns> 61 public int Partition(int[] intArray, int nLower, int nUpper) 62 { 63 int nLeft = nLower + 1; 64 ///以数组第一个元素值作为支点 65 int nPivot = intArray[nLower]; 66 int nRight = nUpper; 67 68 int nSwap; 69 while(nLeft <= nRight) 70 { 71 ///从左向右寻找大于支点元素 72 while(nLeft <= nRight && intArray[nLeft] < nPivot) 73 nLeft++; 74 ///从右向左寻找小于支点元素 75 while(nLeft <= nRight && intArray[nRight] >= nPivot) 76 nRight--; 77 ///交换nLeft和nRight位置元素值 78 if(nLeft < nRight) 79 { 80 nSwap = intArray[nLeft]; 81 intArray[nLeft] = intArray[nRight]; 82 intArray[nRight] = nSwap; 83 nLeft++; 84 nRight--; 85 } 86 } 87 ///以intArray[nRight]为新支点 88 nSwap = intArray[nLower]; 89 intArray[nLower] = intArray[nRight]; 90 intArray[nRight] = nSwap; 91 return nRight; 92 }