Skip to content
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

add snippet to compare two javascript arrays for equality #205

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
15 changes: 14 additions & 1 deletion public/consolidated/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
{
"name": "Array Manipulation",
"snippets": [
{
"title": "Compare Arrays",
"description": "Compares two arrays to check if they are equal.",
"author": "KCSquid",
"tags": [
"array",
"object",
"compare",
"equal"
],
"contributors": [],
"code": "const compareArrays = (a, b) => {\n if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;\n return a.every((v, i) => \n Array.isArray(v) && Array.isArray(b[i]) ? compareArrays(v, b[i]) :\n typeof v === \"object\" && typeof b[i] === \"object\" ? compareObjects(v, b[i]) :\n v === b[i]\n );\n};\n\nconst compareObjects = (a, b) => {\n if (typeof a !== \"object\" || typeof b !== \"object\" || Object.keys(a).length !== Object.keys(b).length) return false;\n return Object.keys(a).every(k => \n Array.isArray(a[k]) && Array.isArray(b[k]) ? compareArrays(a[k], b[k]) :\n typeof a[k] === \"object\" && typeof b[k] === \"object\" ? compareObjects(a[k], b[k]) :\n a[k] === b[k]\n );\n};\n\n// Usage:\ncompareArrays([1, 2, 3], [1, 2, 3]); // Returns: true\ncompareArrays([1, 2, 3], [3, 2, 1]); // Returns: false\ncompareArrays([{a:1}], [{a:1}]); // Returns: true\ncompareArrays([{a:1}], null); // Returns: false\n"
},
{
"title": "Partition Array",
"description": "Splits an array into two arrays based on a callback function.",
Expand Down Expand Up @@ -410,7 +423,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
32 changes: 32 additions & 0 deletions snippets/javascript/array-manipulation/compare-arrays.md
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like this snippet is way too big

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Compare Arrays
description: Compares two arrays to check if they are equal.
author: KCSquid
tags: array,object,compare,equal
---

```js
const compareArrays = (a, b) => {
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
return a.every((v, i) =>
Array.isArray(v) && Array.isArray(b[i]) ? compareArrays(v, b[i]) :
typeof v === "object" && typeof b[i] === "object" ? compareObjects(v, b[i]) :
v === b[i]
);
};

const compareObjects = (a, b) => {
if (typeof a !== "object" || typeof b !== "object" || Object.keys(a).length !== Object.keys(b).length) return false;
return Object.keys(a).every(k =>
Array.isArray(a[k]) && Array.isArray(b[k]) ? compareArrays(a[k], b[k]) :
typeof a[k] === "object" && typeof b[k] === "object" ? compareObjects(a[k], b[k]) :
a[k] === b[k]
);
};

// Usage:
compareArrays([1, 2, 3], [1, 2, 3]); // Returns: true
compareArrays([1, 2, 3], [3, 2, 1]); // Returns: false
compareArrays([{a:1}], [{a:1}]); // Returns: true
compareArrays([{a:1}], null); // Returns: false
```