-2

如何在不使用任何集合和 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';             
}
4

3 回答 3

0

鉴于这个前提,我们可以getComplement像这样创建函数:

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 };
}

然后将其应用于我们现有的 2 个数组以获得所需的结果:

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];

你可以在这里看到一个工作演示

于 2020-10-15T18:03:36.077 回答
0

您可以创建两个列表,一个用于补充,另一个用于差异,迭代数组 A 并检查哪些包含在 B 中,哪些不包含在 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);
于 2020-10-15T15:26:59.087 回答
-2

Acollections和Bare all elements in Athat are not in的区别B。这些集合之间的恭维是所有B不在的元素A。这些是镜像定义,所以你真的只需要写一个差分方法,然后让恭维方法调用差分方法,输入参数反转。

(好吧,严格来说,恭维是所有不在 中的元素A这种区别在这里无关紧要。)

// Get an array of all elements in b that are not in a
// This is identical to calling GetDifference with the inputs reversed so lets just do that
int[] GetCompliment(int[] a, int[] b) { return GetDifference(b, a); }

// Get an array of all elements in a that are not in b
int[] GetDifference(int[] a, int[] b) 
{
    // Create the buffer array at the worst-case length which is the length
    // of a (where none of the elements in a are in b)
    int[] c = new int[a.Length];

    // Track how many elements we are actually ending up with
    int length = 0;

    // Loop through every element in a
    foreach (var ax in a)
    {
        bool found = false;
        // Loop through every element in b to see if it exists in a
        foreach (var bx in b)
        {
            if (ax == bx)
            {
                // If the element was found in b, there's no reason to keep looping
                found = true;
                break;
            }
        }

        // Only save the element if it was not found in b
        if (!found)
        {
            c[length] = ax;
            length++;
        }
    }

    // Create the result array using the length of actual elements found
    int[] result = new int[length];

    // Copy the relevant slice of the buffer array into the result array
    Array.Copy(c, result, length);

    // Return the result array
    return result;
}

用法:

int[] a = { 1, 2, 3, 4 };
int[] b = { 3, 4, 5, 6, 7, 8 };
int[] c = GetDifference(a, b);
        
foreach(var cx in c)
{
    Console.Write(cx + ", ");   
}
        
Console.WriteLine();
int[] d = GetCompliment(a, b);
        
foreach(var dx in d)
{
    Console.Write(dx + ", ");   
}

// Output:
// 1, 2, 
// 5, 6, 7, 8

点网提琴

于 2020-10-15T18:33:07.813 回答