# 查找數組中添加/刪除元素的算法

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:
removed = 7, 2


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 0 are the Added elemnts


## 最佳答案

• if a[x] < b[y], then a[x] was removed (and only increment x)
• if a[x] > b[y], then b[y] was added (and only increment y)

（我可能錯過了一個邊緣案例，但你得到了一般的想法。）

（編輯：這裏沒有涉及的主要邊緣情況是當你在另一個數組之前到達其中一個數組的末尾時處理，但是要弄清楚並不難。:)