[0ms][100%][Fastest Solution Explained] O(n) time complexity | O(n) space complexity
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care brother, pe
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *** Kotlin ***\n\n```\n\nclass Solution {\n    fun fourSum(nums: IntArray, target: Int): List<List<Int>> {\n\tval arrayList = ArrayList<List<Int>>()\n\tnums.sort() // sort necessary to filter out duplicates\n\n\tfor (i in 0 until nums.size) {\n\t\tif (i > 0 && nums[i] == nums[i - 1]) {\n\t\t\tcontinue //since sorted, it is ok to skip and remove duplicates.\n\t\t}\n\n\t\tfor (j in i + 1 until nums.size) {\n\t\t\tif (j > i + 1 && nums[j] == nums[j - 1]) {\n\t\t\t\tcontinue //since sorted, it is ok to skip and remove duplicates.\n\t\t\t}\n\n\t\t\tval friend = target - nums[i] - nums[j]\n\t\t\tvar left = j + 1\n\t\t\tvar right = nums.size - 1\n\n\t\t\twhile (left < right) {\n\t\t\t\tval sumIJ = nums[left] + nums[right]\n\t\t\t\twhen {\n\t\t\t\t\tsumIJ < friend -> left++\n\t\t\t\t\tsumIJ > friend -> right--\n\t\t\t\t\tsumIJ == friend -> {\n\t\t\t\t\t\t//detect duplicates, since they are in order, duplicates will be adjacent.\n\t\t\t\t\t\t//so it is sufficient to compare to previous value\n\t\t\t\t\t\tif (left > j + 1 && right < nums.size - 1) {\n\t\t\t\t\t\t\tif (nums[left] != nums[left - 1] || nums[right] != nums[right + 1]) {\n\t\t\t\t\t\t\t\tarrayList.add(listOf(nums[i], nums[j], nums[left], nums[right]))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tarrayList.add(listOf(nums[i], nums[j], nums[left], nums[right]))\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tleft++\n\t\t\t\t\t\tright--\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t}\n\n\treturn arrayList\n}\n}\n\n```\n\n```\n```\n\n```\n```\n***"We are Anonymous. We are legion. We do not forgive. We do not forget. Expect us. Open your eyes.." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***
['C', 'Java', 'Kotlin', 'JavaScript']