Answer the question
In order to leave comments, you need to log in
What algorithm would you recommend for finding the difference between two arrays?
Good day.
There was a need to create a function in JS, which:
function func(a,b){
// Если старый массив короче значит добавлен 1 элемент в конец массива
if (b.length > a.length) {
return {num: b.length - 1, type: 'add'};
}
// Если старый массив длинее значит удалён 1 элемент
else if (b.length < a.length) {
for (let i = 0; i < a.length; i++) {
if (a[i][0] != b[i][0] || a[i][1] != b[i][1]) {
return {num: i, type: 'delete'};
}
}
// Иначе был изменён элемент
} else {
for (let i = 0; i < a.length; i++) {
if (a[i][0] != b[i][0] || a[i][1] != b[i][1]) {
return {num: i, type: 'change'};
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question