您好,欢迎访问一九零五行业门户网

使用集合在三个数组中找到共同元素的C#程序

设置三个数组
int[] arr1 = { 99, 57, 63, 98};int[] arr2 = { 43, 99, 33, 57};int[] arr3 = { 99, 57, 42};
现在使用hashset设置上述元素。
// hashset onevar h1 = new hashset < int > (arr1);// hashset twovar h2 = new hashset < int > (arr2);// hashset threevar h3 = new hashset < int > (arr3);
让我们来看一下完整的代码来找到共同的元素。
示例using system;using system.collections.generic;using system.linq;public class program { public static void main() { int[] arr1 = { 99, 57, 63, 98 }; int[] arr2 = { 43, 99, 33, 57 }; int[] arr3 = { 99, 57, 42 }; // hashset one var h1 = new hashset < int > (arr1); // hashset two var h2 = new hashset < int > (arr2); // hashset three var h3 = new hashset < int > (arr3); // displaying int[] val1 = h1.toarray(); console.writeline("set one..."); foreach(int val in val1) { console.writeline(val); } //displaying int[] val2 = h2.toarray(); console.writeline("set two..."); foreach(int val in val2) { console.writeline(val); } //displaying int[] val3 = h3.toarray(); console.writeline("set three..."); foreach(int val in val3) { console.writeline(val); } int i = 0, j = 0, k = 0; console.writeline("common elements..."); while (i < val1.length && j < val2.length && k < val3.length) { if (val1[i] == val2[j] && val2[j] == val3[k]) { console.write(val1[i] + " "); i++; j++; k++; } // x < y else if (val1[i] < val2[j]) i++; // y < z else if (val2[j] < val3[k]) j++; else k++; } }}
以上就是使用集合在三个数组中找到共同元素的c#程序的详细内容。
其它类似信息

推荐信息