小编jj.*_*jj.的帖子

查找数组中添加/删除元素的算法

我正在寻找解决以下问题的最有效方法

问题:

given an array Before = { 8, 7, 2, 1} and an array After ={1, 3, 8, 8}
find the added and the removed elements

the solution is:
        added = 3, 8 
        removed = 7, 2
Run Code Online (Sandbox Code Playgroud)

到目前为止我的想法是:

for i = 0 .. B.Lenghtt-1
{
    for j= 0 .. A.Lenght-1
    {
        if A[j] == B[i]

            A[j] = 0;
            B[i] = 0;

            break;
    }
}

// B elemnts different from 0 are the Removed elements
// A elemnts different from …
Run Code Online (Sandbox Code Playgroud)

language-agnostic arrays algorithm

8
推荐指数
3
解决办法
669
查看次数

标签 统计

algorithm ×1

arrays ×1

language-agnostic ×1