Skip to content

Instantly share code, notes, and snippets.

@pdaug
Created April 10, 2025 11:46
Show Gist options
  • Select an option

  • Save pdaug/6f603e39f30af615e6b68cf89e5bd817 to your computer and use it in GitHub Desktop.

Select an option

Save pdaug/6f603e39f30af615e6b68cf89e5bd817 to your computer and use it in GitHub Desktop.
function countDeepDifferences(a: any, b: any): number {
// Se tipos diferentes, ou valor primitivo diferente
if (typeof a !== typeof b || a === null || b === null) {
return a === b ? 0 : 1;
}
// Comparar arrays
if (Array.isArray(a) && Array.isArray(b)) {
const length = Math.max(a.length, b.length);
let count = 0;
for (let i = 0; i < length; i++) {
count += countDeepDifferences(a[i], b[i]);
}
return count;
}
// Se apenas um for array (e o outro não), é diferente
if (Array.isArray(a) !== Array.isArray(b)) {
return 1;
}
// Comparar objetos
if (typeof a === "object" && typeof b === "object") {
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
let count = 0;
for (const key of keys) {
const valA = a[key];
const valB = b[key];
count += countDeepDifferences(valA, valB);
}
return count;
}
// Valor primitivo
return a === b ? 0 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment