JavaScript solution with explanation
We will have 2 loops for 2 elements of quadruplets. Lets say for 2 loops we will use i and j for the interations. For finding next 2 elements we will use 2 poin
We will have 2 loops for 2 elements of quadruplets. Lets say for 2 loops we will use i and j for the interations. For finding next 2 elements we will use 2 pointer method.\nIn 2 pointer method we will have 2 pointers left and right. Initially we will set left pointer as j+1 and right as length-1.\n\nWe will check the sum (nums[i] + nums[j] + nums[left] + nums[right]). If it is equal to target then we will push that quadruplets to result array. We need to increase left and right pointer. Because only changing one pointer will always give different sum (not equal to target)\nIf sum is less than target then we need to increase only left pointer(not the right pointer) because we need to increase the sum value to match it with target value. If we decrease right pointer, we will have smaller element in the combination which will not increase overall sum.(Remember the array is sorted)\nIf sum is greater than the target then we need to reduce right pointer (as per above logic)\n\nTo avoid duplicate entries, we need to take care whether each element is not repeating itself for same combinations of the other elements. Lets say we have array=[3,3,4,4,4,1,1,5] and target=16\n``` \n//first interation \n [3,3,4,4,4,1,1,5]  \n  i j l         r\n  //here sum is 15 which is less than 16. we need to increment the left point. \n  //As next element is also same as current element for left pointer we don\'t need to consider that combination. \n  //We need to increment the left pointer until we get different combination. \n  \n  //next interation should be\n [3,3,4,4,4,1,1,5]  \n  i j       l   r\n```\n\nThis way we need to avoid duplicates for every elements. As the array is sorted all the same elements will be side by side and we can easily avoid using same value for the perticular element.\n  \n```\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[][]}\n */\nvar fourSum = function(nums, target) {\n    nums.sort((a,b)=>a-b)      //First sort the array in ascending order\n    let len = nums.length;\n    let left=0, right=0, sum=0;\n    let result = [];\n    for(let i=0; i<len-3; i++){\n        for(let j=i+1; j<len-2; j++){\n            left = j+1;\n            right = len-1;\n            while(left < right){\n                sum = nums[i] + nums[j] + nums[left] + nums[right];\n                if(sum === target){\n                    result.push([nums[i], nums[j], nums[left], nums[right]])\n                    while(nums[left]===nums[left+1]) left++;     //To avoid same values for left pointer\n                    while(nums[right]===nums[right-1]) right--;  //To avoid same values for right pointer\n                    left++;\n                    right--;\n                } else if (sum < target){\n                    left++;\n                } else {\n                    right--;\n                }\n            }\n            while(nums[j]===nums[j+1]) j++;\n        }\n        while(nums[i]===nums[i+1]) i++;\n    }\n    return result;\n};\n```\n\nTime complexity will be O(n^3) as there are 2 loops and in worst case left pointer will go till end of the array.\nSpace complexity will be O(1) as we are not taking any extra space to store anything.\n
['Two Pointers', 'JavaScript']