Java O(n) Sliding Window With HashSet With Photos and Explanation
\n\n\n\n\n\n\n\n\n\n\n\n```\nclass Solution {\n   \tpublic static int maximumUniqueSubarray(int[] nums) {\n\n        // HashSet to check if the next value is un
![image](https://assets.leetcode.com/users/images/e340a43f-58d3-4469-a601-08944adc916e_1655042888.5296369.jpeg)\n\n\n\n\n\n\n\n\n\n\n\n```\nclass Solution {\n   \tpublic static int maximumUniqueSubarray(int[] nums) {\n\n        // HashSet to check if the next value is unique.\n\t\tSet<Integer> set = new HashSet<>();\n        \n        // maxSum for the maximum score that we can get \n\t\tint maxSum = 0;\n        // currSum is used for the current sum of the iteration \n\t\tint currSum = 0;\n\n        // We have two pointer left and right \n        // r pointer will traverse the array and l will stay to indicate inital \n        // value for our current case \n\t\tfor (int l = 0, r = 0; r < nums.length; r++) {\n            // let\'s add our next value \n\t\t\tcurrSum += nums[r];\n        // if the next element is already in the current set \n        // we need to move our l pointer \n    \t\t\tif (set.contains(nums[r])) {\n        // delete the values until we have found the non-unique value \n\t\t\t\twhile (nums[l] != nums[r]) {\n\t\t\t\t\tcurrSum -= nums[l];\n\t\t\t\t\tset.remove(nums[l]);\n\t\t\t\t\tl++;\n\t\t\t\t}\n        // now we have found the non-unique value in our current case \n        // lets remove it both currSum and our set \n\t\t\t\tif (nums[l] == nums[r]) {\n\t\t\t\t\tcurrSum -= nums[l];\n\t\t\t\t\tset.remove(nums[l]);\n\t\t\t\t\tl++;\n\t\t\t\t}\n\t\t\t}\n        // add current value to the set \n\t\t\tset.add(nums[r]);\n        // compare the current case with previous cases to get maximum value .\n\t\t\tmaxSum = Math.max(currSum, maxSum);\n\n\t\t}\n\t\treturn maxSum;\n\t}\n}
['Sliding Window', 'Java']