Java | Sliding Window | Easy to Understand
Use a Sliding window to calculate the maximum sum subarray which has all unique elements. We can use a set to maintain the uniqueness.\n\n\nclass Solution {\n  
Use a Sliding window to calculate the maximum sum subarray which has all unique elements. We can use a set to maintain the uniqueness.\n\n```\nclass Solution {\n    public int maximumUniqueSubarray(int[] nums) {\n        Set<Integer> set = new HashSet<>();\n        \n        int start = 0, n = nums.length, sum = 0, answer = -1;\n        \n        for(int end = 0; end < n; end++) {\n            // If the window has duplicate elements then shrink the window from the left\n            if(set.contains(nums[end])) {\n                while(set.contains(nums[end])) {\n                    sum -= nums[start];\n                    set.remove(nums[start]);\n                    start++;\n                }\n            }\n            // Expand the window from the right\n            set.add(nums[end]);\n            sum += nums[end];\n            // Calculate the max window sum\n            answer = Math.max(answer, sum);\n        }\n        \n        return answer;\n    }\n}\n```
['Sliding Window', 'Ordered Set', 'Java']