计算两个数组中不是commen的元素数 [英] count the numbers of elements which r not commen in both array

查看:93
本文介绍了计算两个数组中不是commen的元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在哪个逻辑可以包含在distinctElementCount方法中

now which logic can include in distinctElementCount method

int distinctElementCount(int *arr1, int *arr2, int len1, int len2)
{
    // COUNT ELEMENTS WHICH ARE NOT COMMON IN BOTH THE ARRAYS
    // WRITE YOUR CODE HERE
}
// FUNCTION SIGNATURE ENDS

// DO NOT IMPLEMENT THE main( ) METHOD
int main()
{
    // DO NOT CHANGE ANY CODE IN THIS FUNCTION
    int len1,len2;
    int result;
    int arr1[len1];
    int arr2[len2];
    //ASSUME INPUT HAVE ALREADY BEEN TAKEN
    result = distinctElementCount(arr1,arr2,len1,len2);
    return 0;
}
int distinctElementCount(arr1,arr2,len1,len2)
{

}

推荐答案

这有很多功课,所以我不会给你任何代码。



但是如果你先做一件事就很容易:对你的输入进行排序(如果需要的话,对另一对数组进行排序)。

然后你可以遍历两个数组:如果元素是相同的,不计算它们,但移动两个索引。如果一个低于另一个,则计算它,并仅将该索引移动到一个。



简单!首先在纸上试一试,你会看到我的意思。
This smells heavily of homework, so I won't give you any code.

But it';s pretty easy if you do one thing first: sort both your inputs (into another pair of arrays if necessary).
You can then loop through both arrays: if the elements are the same, don't count them, but move both indexes. If one is lower than the other, count it, and move that index only on one.

Easy! Try it on paper first, and you will see what I mean.


这篇关于计算两个数组中不是commen的元素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆