✅ JS || Multiple Approaches || Easy to understand
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com/2022/10/4-sum.html\nIt has solutions to almos
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com/2022/10/4-sum.html\nIt has solutions to almost every problem on Leetcode, and I recommend checking it out.\nNote: You can bookmark it as a resource, and approach. Other approaches are in above blog\n<br>\n\n```\nvar fourSum = function (nums, target) {\n    let solutionArrays = [];\n\n    // Sort the array\n    sortedNums = nums.sort((a, b) => a - b);\n\n    // Loop through the array (excluding the last 3 entries)\n    for (let i = 0; i < sortedNums.length - 3; i++) {\n        // Check if this is the same number as the last one\n        if (i > 0 && sortedNums[i] == sortedNums[i - 1]) {\n            continue;\n        }\n\n        // Loop through the remaining numbers\n        for (let j = i + 1; j < sortedNums.length - 2; j++) {\n            // Check if this is the same number as the last one\n            if (j > i + 1 && sortedNums[j] == sortedNums[j - 1]) {\n                continue;\n            }\n\n            // Establish a window from the next number to the end of the array\n            let left = j + 1;\n            let right = sortedNums.length - 1;\n\n            // Run until the window is closed\n            while (left < right) {\n                // Add together the current numbers, and each side of the window\n                const total =\n                    sortedNums[i] +\n                    sortedNums[j] +\n                    sortedNums[left] +\n                    sortedNums[right];\n\n                // If we\'ve reached an answer, add it to the array of answers\n                if (total == target) {\n                    solutionArrays.push([\n                        sortedNums[i],\n                        sortedNums[j],\n                        sortedNums[left],\n                        sortedNums[right],\n                    ]);\n\n                    // Move the left-hand edge of the window to the right until it reaches a different number\n                    do {\n                        left++;\n                    } while (sortedNums[left] == sortedNums[left - 1]);\n\n                    // Move the right-hand edge of the window to the left until it reaches a different number\n                    do {\n                        right--;\n                    } while (sortedNums[right] == sortedNums[right + 1]);\n                }\n                // If we\'re too low\n                else if (total < target) {\n                    left++;\n                }\n                // If we\'re too high\n                else {\n                    right--;\n                }\n            }\n        }\n    }\n\n    return solutionArrays;\n};\n```
['JavaScript']