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 (array1.length !== array2.length) return false;\n\n for (let i = 0; i < array1.length; i++) {\n if (array1[i] !== array2[i]) {\n return false;\n }\n }\n\n return true;\n}\n\n// Usage:\narray1 = [1, 2, 3, 4, 5];\narray2 = [5, 4, 3, 2, 1];\ncompareArrays(array1, array2); // 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
25 changes: 25 additions & 0 deletions snippets/javascript/array-manipulation/compare-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
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 (array1.length !== array2.length) return false;

for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}

return true;
}

// Usage:
array1 = [1, 2, 3, 4, 5];
array2 = [5, 4, 3, 2, 1];
compareArrays(array1, array2); // Returns: false
```
Loading