# C/C++ 统计两个数组中不同元素的个数

C/C++ 统计两个数组中不同元素的个数

//统计两个数组中不同元素的个数

//数组中不含重复元素
#include "stdafx.h"
#include "stdio.h"
#include"stdlib.h"
#include<iostream>

using namespace std;

int Match2Array(int * pArray1, int len1, int * pArray2, int len2)
{
for (int i = 0; i < len1-1; i++)
{
for (int j = 0; j < len1-i-1; j++)
{
if (pArray1[j]>pArray1[j + 1])
{
int temp = pArray1[j];
pArray1[j] = pArray1[j + 1];
pArray1[j + 1] = temp;
}
}
}
for (int i = 0; i < len2 - 1; i++)
{
for (int j = 0; j < len2 - i - 1; j++)
{
if (pArray2[j]>pArray2[j + 1])
{
int temp = pArray2[j];
pArray2[j] = pArray2[j + 1];
pArray2[j + 1] = temp;
}
}
}
//至此，两个数组都变成了有序数组，从小到大排列
int *p1 = pArray1 + len1;
int *p2 = pArray2 + len2;
int num = 0;//相同元素的个数

while (pArray1 < p1&&pArray2 < p2)
{
if (*pArray1 == *pArray2)
{
num++;
pArray1++;
pArray2++;
}
else if (*pArray1 < *pArray2)
{
pArray1++;
continue;
}
else
{
pArray2++;
continue;
}
}
return len1 + len2 - 2 * num;

}
int main()
{
int f[4] = { 1, 3, 4, 7 };
int g[5] = { 2, 3, 5, 7, 8 };
int size=Match2Array(f, 4, g, 5);

cout << size << endl;

return 0;
}

//只含一个重复元素时
#include "stdafx.h"
#include "stdio.h"
#include"stdlib.h"
#include<iostream>

using namespace std;

//统计数组中相同元素的个数
int *reNum(int *p, int size)
{
int arr[10] = { 0 };

for (int i = 0; i < size;i++)
{
arr[p[i]]++;
}
return arr;
}
int Match2Array(int * pArray1, int len1, int * pArray2, int len2)
{
for (int i = 0; i < len1 - 1; i++)
{
for (int j = 0; j < len1 - i - 1; j++)
{
if (pArray1[j]>pArray1[j + 1])
{
int temp = pArray1[j];
pArray1[j] = pArray1[j + 1];
pArray1[j + 1] = temp;
}
}
}
for (int i = 0; i < len2 - 1; i++)
{
for (int j = 0; j < len2 - i - 1; j++)
{
if (pArray2[j]>pArray2[j + 1])
{
int temp = pArray2[j];
pArray2[j] = pArray2[j + 1];
pArray2[j + 1] = temp;
}
}
}
int *b, *c;
b = reNum(pArray1, len1);
c = reNum(pArray2, len2);
//至此，两个数组都变成了有序数组，从小到大排列
int *p1 = pArray1 + len1;
int *p2 = pArray2 + len2;
int num = 0;//相同元素的个数
int f;
while (pArray1 < p1&&pArray2 < p2)
{
if (*pArray1 == *pArray2)
{
num++;
f = *pArray1;
pArray1++;
pArray2++;
}
else if (*pArray1 < *pArray2)
{
pArray1++;
continue;
}
else
{
pArray2++;
continue;
}
}

num = num+b[f]+c[f];

return len1 + len2 - num;

}
int main()
{
int f[5] = { 1, 3, 3, 4, 7 };
int g[6] = { 2, 3, 5, 9, 8 ,6 };
int size=Match2Array(f, 5, g, 6);
/*int f[3] = { 1, 3, 3};
int g[3] = { 2, 3, 5 };
int size = Match2Array(f, 3, g, 3);*/
cout << size << endl;

return 0;
}


//数组中包含多个重复元素
#include "stdafx.h"
#include "stdio.h"
#include"stdlib.h"
#include<iostream>

using namespace std;

//统计数组中相同元素的个数
int *reNum(int *p, int size)
{
int arr[10] = { 0 };

for (int i = 0; i < size;i++)
{
arr[p[i]]++;
}
return arr;
}
int Match2Array(int * pArray1, int len1, int * pArray2, int len2)
{
for (int i = 0; i < len1 - 1; i++)
{
for (int j = 0; j < len1 - i - 1; j++)
{
if (pArray1[j]>pArray1[j + 1])
{
int temp = pArray1[j];
pArray1[j] = pArray1[j + 1];
pArray1[j + 1] = temp;
}
}
}
for (int i = 0; i < len2 - 1; i++)
{
for (int j = 0; j < len2 - i - 1; j++)
{
if (pArray2[j]>pArray2[j + 1])
{
int temp = pArray2[j];
pArray2[j] = pArray2[j + 1];
pArray2[j + 1] = temp;
}
}
}
int *b, *c;
b = reNum(pArray1, len1);
c = reNum(pArray2, len2);
//至此，两个数组都变成了有序数组，从小到大排列
int *p1 = pArray1 + len1;
int *p2 = pArray2 + len2;
int num = 0;//相同元素的个数
int k=0,f[10] = { 0 };

while (pArray1 < p1&&pArray2 < p2)
{
if (*pArray1 == *pArray2)
{
num++;
f[k++] = *pArray1;
pArray1++;
pArray2++;
}
else if (*pArray1 < *pArray2)
{
pArray1++;
continue;
}
else
{
pArray2++;
continue;
}
}
for (int kk = 0; kk < sizeof(f) / sizeof(f[0]); kk++)
{
num = num + b[f[kk]] + c[f[kk]];
}

return len1 + len2 - num;

}
int main()
{
int f[5] = { 1, 3, 3, 7, 7 };
int g[6] = { 2, 3, 5, 7, 8 ,6 };
int size=Match2Array(f, 5, g, 6);
/*int f[3] = { 1, 3, 3};
int g[3] = { 2, 3, 5 };
int size = Match2Array(f, 3, g, 3);*/
cout << size << endl;

return 0;
}


int arr[10] = { 0 };

• 本文已收录于以下专栏：

举报原因： 您举报文章：C/C++ 统计两个数组中不同元素的个数 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)