Title: Three Sum in JavaScript - Runnable Code Snippet
Open Graph Title: Three Sum in JavaScript - Runnable Code Snippet
X Title: Three Sum in JavaScript - Runnable Code Snippet
Description: Find all unique triplets in an array that sum to a target value. Runnable JavaScript snippet with live execution.
Open Graph Description: Find all unique triplets in an array that sum to a target value. Runnable JavaScript snippet with live execution.
X Description: Find all unique triplets in an array that sum to a target value. Runnable JavaScript snippet with live execution.
Keywords:
Opengraph URL: https://tempercode.dev/snippets/three-sum-javascript
Domain: tempercode.dev
{"@context":"https://schema.org","@type":"SoftwareSourceCode","name":"Three Sum","description":"Find all unique triplets in an array that sum to a target value.","programmingLanguage":"JavaScript","text":"const a = [...arr].sort((x, y) => x - y);\nconst result = [];\nfor (let i = 0; i < a.length - 2; i++) {\n if (i > 0 && a[i] === a[i - 1]) continue;\n let l = i + 1, r = a.length - 1;\n while (l < r) {\n const sum = a[i] + a[l] + a[r];\n if (sum === target) {\n result.push([a[i], a[l], a[r]]);\n while (l < r && a[l] === a[l + 1]) l++;\n while (l < r && a[r] === a[r - 1]) r--;\n l++; r--;\n } else if (sum < target) l++;\n else r--;\n }\n}\nreturn result;"}
| theme-color | #0c0d12 |
| color-scheme | dark |
| author | Temper |
| creator | Temper |
| publisher | Temper |
| og:image | https://tempercode.dev/og-image.png |
| og:type | article |
| twitter:card | summary_large_image |
| twitter:image | https://tempercode.dev/og-image.png |
Links:
Viewport: width=device-width, initial-scale=1