C# 集合运算(补和差)

C# 集合运算(补和差),c#,set,set-operations,C#,Set,Set Operations,在不使用任何集合和Linq的情况下,如何在C#中进行集补码和集差? 我们有两个阵列: int [] arr1 = new int { 1, 2, 3, 4}; int[] arr2 = new int {3,4,5,6,7,8}; 补码必须是:arr3{5,6,7,8},差分必须是:arr4{1,2} 我试着将一组添加到另一组,然后找到重复的,但没有成功 int numDups = 0, prevIndex = 0; for (int i = 0; i < array.Length;

在不使用任何集合和Linq的情况下,如何在C#中进行集补码和集差?

我们有两个阵列:

int [] arr1 = new int { 1, 2, 3, 4};
int[] arr2 = new int {3,4,5,6,7,8};
补码必须是:
arr3{5,6,7,8}
,差分必须是:
arr4{1,2}

我试着将一组添加到另一组,然后找到重复的,但没有成功

int numDups = 0, prevIndex = 0;

for (int i = 0; i < array.Length; i++)
{
    bool foundDup = false;
    for (int j = 0; j < i; j++)
    {
        if (array[i] == array[j])
        {
            foundDup = true;
            numDups++; // Increment means Count for Duplicate found in array.
            break;
        }                    
    }

    if (foundDup == false)
    {
        array[prevIndex] = array[i];
        prevIndex++;
    }
}

// Just Duplicate records replce by zero.
for (int k = 1; k <= numDups; k++)
{               
    array[array.Length - k] = '\0';             
}
int numDups=0,prevIndex=0;
for(int i=0;i对于(int k=1;k您可以创建两个列表,一个用于补码,另一个用于差分,迭代数组A并检查B中包含哪些,反之亦然,迭代B并检查A中存在哪些

更新:删除列表,仅使用阵列,不使用LINQ

int[] arr1 = new int[]{ 1,2,3,4 };
int[] arr2 = new int[]{ 3,4,5,6,7,8 };

//We allocate the max possible size for each array, just for sanity
int[] arr3 = new int[arr1.Length + arr2.Length];
int[] arr4 = new int[arr1.Length + arr2.Length];

int difIndex = 0;
int compIndex = 0;

//Compute difference 
foreach(int i in arr1)
{
    bool found = false;
    foreach(int i2 in arr2)
    {
        if(i == i2)
        {
            found = true;
            break;
        }
    }

    if(!found)
        arr4[difIndex++] = i;
}

//Compute complement
foreach(int i in arr2)
{
    bool found = false;
    foreach(int i2 in arr1)
    {
        if(i == i2)
        {
            found = true;
            break;
        }
    }

    if(!found)
        arr3[compIndex++] = i;
}

//Remove unused items from array
Array.Resize(ref arr3, compIndex);
Array.Resize(ref arr4, difIndex);

基于这个前提,我们可以创建函数
getcomplete
,如下所示:

int[][] getComplement(int[] arr1, int[] arr2) {
    
    int[] complement = {};
    int[] difference = {};

    for (int i = 0; i < arr1.Length; i++)
    {
        bool isDupe = false;
        for (int j = 0; j < arr2.Length; j++) {
            if (arr1[i] == arr2[j] && !isDupe) {
                Array.Resize(ref complement, complement.Length + 1);
                complement[complement.GetUpperBound(0)] = arr2[j];
                isDupe = true;
            }
        }
        if (!isDupe) {
            Array.Resize(ref difference, difference.Length + 1);
            difference[difference.GetUpperBound(0)] = arr1[i];
        }
    }
    
    return new[] { complement, difference };
}

您可以看到。

集合
A
B
之间的区别在于
A
中的所有元素都不在
B
中。这些集合之间的互补之处在于
B
中的所有元素都不在
A
中。这些是镜像定义,因此您确实只需要为这些元素编写一个方法然后让恭维方法调用差分方法,并反转输入参数

(严格地说,恭维是指不在
A
中的所有元素,但这种区别在这里并不重要。)

//获取b中不在a中的所有元素的数组
//这与在输入反转的情况下调用GetDifference是相同的,所以我们就这么做吧
int[]getcompaim(int[]a,int[]b){return GetDifference(b,a);}
//获取a中不在b中的所有元素的数组
int[]GetDifference(int[]a,int[]b)
{
//以最坏情况下的长度(即长度)创建缓冲区数组
//a的(其中a中的任何元素都不在b中)
int[]c=新的int[a.长度];
//追踪我们到底有多少元素
整数长度=0;
//循环一个元素中的每个元素
foreach(a中的var ax)
{
bool-found=false;
//循环遍历b中的每个元素,查看它是否存在于a中
foreach(b中的变量bx)
{
如果(ax==bx)
{
//如果元素是在b中找到的,那么就没有理由继续循环
发现=真;
打破
}
}
//仅在b中找不到元素时保存该元素
如果(!找到)
{
c[长度]=ax;
长度++;
}
}
//使用找到的实际元素的长度创建结果数组
int[]结果=新的int[长度];
//将缓冲区数组的相关切片复制到结果数组中
数组.Copy(c,结果,长度);
//返回结果数组
返回结果;
}
用法:

int[]a={1,2,3,4};
int[]b={3,4,5,6,7,8};
int[]c=GetDifference(a,b);
foreach(c中的变量cx)
{
控制台。写入(cx+“,”);
}
Console.WriteLine();
int[]d=getcompaign(a,b);
foreach(d中的变量dx)
{
控制台。写入(dx+“,”);
}
//输出:
// 1, 2, 
// 5, 6, 7, 8

不幸的是,我既不能使用System.Collections.Generic也不能使用System.Collections.Generic.List.Linq也被禁止(((这是task@InfernalDiablo更新
int [] arr1 = new int[] { 1, 2, 3, 4 };
int[] arr2 = new int[] { 3, 4, 5, 6, 7, 8 };
int[][] complementArr = getComplement(arr1, arr2);
int[][] differenceArr = getComplement(arr2, complementArr[0]);
int[] arr3 = differenceArr[1];
int[] arr4 = complementArr[1];