Skip to content

add snippet to compare two javascript arrays for equality #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 17, 2025
14 changes: 13 additions & 1 deletion public/consolidated/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
{
"name": "Array Manipulation",
"snippets": [
{
"title": "Compare Arrays",
"description": "Compares two arrays to check if they are equal.",
"author": "KCSquid",
"tags": [
"array",
"compare",
"equal"
],
"contributors": [],
"code": "const compareArrays = (array1, array2) => {\n if (\n !Array.isArray(array1) || !Array.isArray(array2) ||\n array1.length !== array2.length\n ) return false;\n\n for (let i = 0; i < array1.length; i++) {\n if (Array.isArray(array1[i]) && Array.isArray(array2[i])) {\n if (!compareArrays(array1[i], array2[i])) return false;\n } else if (typeof array1[i] === \"object\" && typeof array2[i] === \"object\") {\n if (!compareObjects(array1[i], array2[i])) return false;\n } else if (array1[i] !== array2[i]) {\n return false;\n }\n }\n\n return true;\n};\n\nconst compareObjects = (obj1, obj2) => {\n if (typeof obj1 !== \"object\" || typeof obj2 !== \"object\") return false;\n\n const keys1 = Object.keys(obj1);\n const keys2 = Object.keys(obj2);\n if (keys1.length !== keys2.length) return false;\n\n for (let key of keys1) {\n const val1 = obj1[key];\n const val2 = obj2[key];\n\n if (Array.isArray(val1) && Array.isArray(val2)) {\n if (!compareArrays(val1, val2)) return false;\n } else if (typeof val1 === \"object\" && typeof val2 === \"object\") {\n if (!compareObjects(val1, val2)) return false;\n } else if (val1 !== val2) {\n return false;\n }\n }\n\n return true;\n};\n\n// Usage:\nconst number = 123;\nconst array1 = [1, 2, 3, 4, 5];\nconst array2 = [1, 2, 3, 4, 5];\nconst array3 = [[1, 2], [3, 4]];\nconst array4 = [[1, 2], [3, 4]];\nconst array5 = [{ a: 1, b: [{ c: 2 }] }, 3];\nconst array6 = [{ a: 1, b: [{ c: 2 }] }, 3];\n\ncompareArrays(array1, array2); // Returns: true\ncompareArrays(array3, array4); // Returns: true\ncompareArrays(array5, array6); // Returns: true\ncompareArrays(array1, number); // Returns: false\ncompareArrays(array3, array6); // Returns: false\n"
},
{
"title": "Partition Array",
"description": "Splits an array into two arrays based on a callback function.",
Expand Down Expand Up @@ -410,7 +422,7 @@
"algebra"
],
"contributors": [],
"code": "function combinations(n, r) {\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n return factorial(n) / (factorial(r) * factorial(n - r));\n}\n\n// Usage:\ncombinations(12,24); // Returns: 7.720248753351544e-16\ncombinations(1,22); // Returns: 8.896791392450574e-22\n"
"code": "function combinations(n, r) {\n if (n < 0 || r < 0 || n < r) {\n throw new Error('Invalid input: n and r must be non-negative and n must be greater than or equal to r.');\n }\n\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n\n const numerator = factorial(n);\n const denominator = factorial(r) * factorial(n - r);\n return numerator / denominator;\n}\n\n// Usage:\ncombinations(24,22); // Returns: 276\ncombinations(5,3); // Returns: 10\n"
},
{
"title": "Cross Product",
Expand Down
65 changes: 65 additions & 0 deletions snippets/javascript/array-manipulation/compare-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Compare Arrays
description: Compares two arrays to check if they are equal.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like the description to tell that it compares deeply two arrays since we added that

author: KCSquid
tags: array,compare,equal
---

```js
const compareArrays = (array1, array2) => {
if (
!Array.isArray(array1) || !Array.isArray(array2) ||
array1.length !== array2.length
) return false;

for (let i = 0; i < array1.length; i++) {
if (Array.isArray(array1[i]) && Array.isArray(array2[i])) {
if (!compareArrays(array1[i], array2[i])) return false;
} else if (typeof array1[i] === "object" && typeof array2[i] === "object") {
if (!compareObjects(array1[i], array2[i])) return false;
} else if (array1[i] !== array2[i]) {
return false;
}
}

return true;
};

const compareObjects = (obj1, obj2) => {
if (typeof obj1 !== "object" || typeof obj2 !== "object") return false;

const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;

for (let key of keys1) {
const val1 = obj1[key];
const val2 = obj2[key];

if (Array.isArray(val1) && Array.isArray(val2)) {
if (!compareArrays(val1, val2)) return false;
} else if (typeof val1 === "object" && typeof val2 === "object") {
if (!compareObjects(val1, val2)) return false;
} else if (val1 !== val2) {
return false;
}
}

return true;
};

// Usage:
const number = 123;
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4, 5];
const array3 = [[1, 2], [3, 4]];
const array4 = [[1, 2], [3, 4]];
const array5 = [{ a: 1, b: [{ c: 2 }] }, 3];
const array6 = [{ a: 1, b: [{ c: 2 }] }, 3];

compareArrays(array1, array2); // Returns: true
compareArrays(array3, array4); // Returns: true
compareArrays(array5, array6); // Returns: true
compareArrays(array1, number); // Returns: false
compareArrays(array3, array6); // Returns: false
```