C++ 查找存在于第一个数组而不存在于第二个数组中的元素

C++ 查找存在于第一个数组而不存在于第二个数组中的元素,c++,arrays,algorithm,C++,Arrays,Algorithm,例如: 输入: 10 20 30 40 50 60 70 80 90 100 10 20 30 40 50 50 40 30 20 10 1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 输出: 60 70 80 90 100 1 2 3 4 5 6 7 8 9 10 60 70 80 90 100 输入: 10 20 30 40 50 60 70 80 90 100 10 20 30 40 50 50 40 30 20 1

例如:

输入:

10 20 30 40 50 60 70 80 90 100

10 20 30 40 50 50 40 30 20 10
1 2 3 4 5 6 7 8 9 10

-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
输出:

60 70 80 90 100
1 2 3 4 5 6 7 8 9 10
60 70 80 90 100

输入:

10 20 30 40 50 60 70 80 90 100

10 20 30 40 50 50 40 30 20 10
1 2 3 4 5 6 7 8 9 10

-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
输出:

60 70 80 90 100
1 2 3 4 5 6 7 8 9 10
60 70 80 90 100

我的代码:

#include <iostream>

int main()
{
    const int n = 10;
    int arr1[n];
    const int m = 10;
    int arr2[n];
    int uncommon = 0;
    for (int i = 0; i < n; ++i)
    {
        std::cin >> arr1[i];
    }
    for (int j = 0; j < m; ++j)
    {
        std::cin >> arr2[j];
    }
    for (int i = 0; i < n; ++i)
    {
        count = 0;
        for (int j = 0; j < m; ++j)
        {
            if (arr1[i] == arr2[j])
                uncommon++;
        }
        if (uncommon == 0)
            std::cout << arr1[i];
    }
}
#包括
int main()
{
常数int n=10;
int-arr1[n];
常数int m=10;
int-arr2[n];
int=0;
对于(int i=0;i>arr1[i];
}
对于(int j=0;j>arr2[j];
}
对于(int i=0;iSTD::CUT

我有点忘记了C++,但是这里有pSdoDo代码(实际上,JavaScript):

notPresent=[];
对于(i=0;i使用和可用于输出两个数组中的差异:

#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int a[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
    int b[] = { 10, 20, 30, 40, 50, 50, 40, 30, 20, 10 };

    // sort the two arrays
    std::sort(std::begin(a), std::end(a));
    std::sort(std::begin(b), std::end(b));

    // get the set_difference and output the results
    std::set_difference(std::begin(a), std::end(a),
        std::begin(b), std::end(b), std::ostream_iterator<int>(std::cout, " "));
}
#包括
int main()
{
常数int n=10;
int arr1[n],arr2[n];
对于(int i=0;i>arr1[i];
}
对于(int j=0;j>arr2[j];
}
对于(int i=0;istd::cout我提供以下解决方案:

  • 运行时:O(n*m),n-arr1的大小,m-arr2的大小。内存:O(1) Idea:第一个循环用于arr1中的元素,第二个循环用于arr2中的元素。如果元素匹配,则可以使用值(可能是-1 000 000或其他值)更改arr1中的元素,这将告诉您此元素在arr2中显示。示例: arr1:[1,2,3],arr2:[10,12,1]。然后在解决方案之后,arr1将是:[-1000000,2,3]。它不需要额外的内存,因为所有操作都是就地执行的
  • 代码

    int main()
    {
        const int n = 10;
        int arr1[n], arr2[n];
    
        for(int i = 0; i < n; ++i)
            { 
            std::cin >> arr1[i]; 
            }
        for(int j = 0; j < n; ++j)
            {
            std::cin >> arr2[j];
            }
        const int value = -1000000; // Better to use INT_MAX or INT_MIN
        for(int i = 0; i < n; ++i)
            {
            for(int j = 0; j < m; ++j) // You should iterate through all elements 
                                      //in arr2 which size is m.
                if(arr1[i] == arr2[j])
                    arr1[i] = value; 
            }
        // Print elements which is in arr1 and not in arr2
           for (int i = 0; i < n; i++){
               std::cout << arr1[i] << ' ';
           }
    }
    
    intmain()
    {
    常数int n=10;
    int arr1[n],arr2[n];
    对于(int i=0;i>arr1[i];
    }
    对于(int j=0;j>arr2[j];
    }
    const int value=-1000000;//最好使用int\u MAX或int\u MIN
    对于(int i=0;istd::cout如果您正在寻找内存有效的解决方案,即内存的O(1),那么您可以对第二个数组进行排序,并通过二进制搜索查找第一个数组中的每个元素是否存在于第二个数组中。时间复杂度:
    O(mlogm+nlogm)
    其中n是第一个数组的大小,m是第二个数组的大小。

    代码有什么问题?你有什么问题?使用
    set
    unordered\u set
    。什么是
    count
    ?这是打字错误吗?)如果数组被排序,请查看:“从排序范围复制元素
    [first1,last1)
    在排序范围内找不到的
    [first2,last2)
    到从
    d\u first开始的范围
    “如果我的答案对您有帮助,请选择并向上投票。谢谢!