查找第一个数组中不存在的元素

Find elements which are present in the first array and not in the second

本文关键字:元素 不存在 第一个 数组 查找      更新时间:2023-10-16

如何从给定的两个数组中找到第二个数组中不存在的数字C++?

例如:

输入:

10 20 30 40 50 60 70 80 90 100
10 20 30 40 50 50 40 30 20 10

输出:

60 70 80 90 100
<小时 />

输入:

1 2 3 4 5 6 7 8 9 10
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10

输出:

1 2 3 4 5 6 7 8 9 10

我的代码:

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

std::sort 和 std::set_difference 的使用可用于输出两个数组中的差值:

#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, " "));
}

输出:

60 70 80 90 100

我有点忘记了c ++,但这里是psudo代码(实际上是javascript(:

notPresent=[];
for(i=0;i<array1.length;i++) {
found=false;
for(j=0;j<array2.length && !found;j++) {
if(array1[i]==array2[j]) found=true;
};
if(!found) {
notPresent.push(array1[i]);
}
}

这个想法是:循环访问第一个数组的元素并与第二个数组的所有元素进行比较(一旦找到就停止(,如果没有找到:缺少值,根据要求,推送到 notPresent 数组。

我提供以下解决方案:

  1. 运行时间:O(n * m(,n - arr1 的大小,m - arr2 的大小。内存: O(1(想法:第一个循环用于 arr1 中的元素,第二个循环用于 arr2 中的元素。如果元素匹配,那么您可以使用值(可能是 -1 000 000 或其他值(更改 arr1 中的元素,这将告诉您该元素显示在 arr2 中。例: 到达1 : [1, 2, 3], 到达2 : [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] << ' ';
}
}
  1. 运行时:O(n*m(。内存: O(元素的大小在 arr1 中而不是在 arr2 中(想法:同样在这里,创建一个数组,显示哪些元素在 arr1 中,而不是在 arr2 中。然后遍历 arr1 的元素并与 arr2 的元素进行比较。如果元素与 arr2 的每个元素都不匹配,您可以保存地将其推送到 arr1。

法典:

#include <iostream>

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];
}
vector<int> Array;
for(int i = 0; i < n; ++i){
int flag = 0; // flag initially 0 for every element in arr1
for(int j = 0; j < m; ++j){
if(arr1[i] == arr2[j])  // If it's match you make flag = 1
flag = 1;
}
if (flag == 0){    // It shows you that where wasn't match
Array.push_back(arr1[i]);
}
}
for (int i = 0; i < Array.size(); i++){
std::cout << Array[i] << ' ';
}

}
  1. 运行时:O(n( + O(m(。内存:O(n( **想法: **使用数据结构图。(为了理解它,我建议您在YouTube中搜索。有很多好视频(。尝试思考和实施自己(

如果您正在寻找内存效率的解决方案,即内存的 O(1(,那么您可以对第二个数组进行排序,并为第一个数组中的每个元素查找它是否存在于第二个数组中通过二进制搜索。时间复杂度:O(mlogm + nlogm)其中n是第一个数组的大小,m是第二个数组的大小。

#include <iostream>

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

for(int i = 0; i < n; ++i)
{
int j;
for(j = 0; j < n; ++j)
if(arr1[i] == arr2[j])
break;
if(j == n)
std::cout << arr1[i] << " ";
}
}