sto cercando il modo più efficiente per risolvere il seguente/elementi Algoritmo per trovare aggiunto rimossi in un array
Problema:
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
La mia idea finora è:
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
Qualcuno sa una soluzione migliore forse più efficace e che non sovrascrive gli array originali
Se 3, 8 sono aggiunti e 7, 2, 1 vengono rimossi poi la "matrice Dopo" dovrebbe essere '{3, 8, 8}' o '{1, 3, 8, 8}'. – kennytm
Un "1" non può esistere nella matrice "Dopo". Si inizia con un solo "1", lo si rimuove e non lo si aggiunge, quindi perché è presente nell'array After? Dovresti correggere il tuo esempio. –
i miei soliti errori, l'ho corretto. Thx, jj –