MCPcopy Create free account
hub / github.com/DestinationFAANG/Destination-FAANG-Java-Solution / Solution

Class Solution

90 Subsets II/90 Subsets II.java:10–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9
10class Solution {
11 public List<List<Integer>> subsetsWithDup(int[] nums) {
12 List<List<Integer>> result = new ArrayList<>();
13 Arrays.sort(nums); // Sort the array to handle duplicates
14 backtrack(result, new ArrayList<>(), nums, 0);
15 return result;
16 }
17
18 private void backtrack(List<List<Integer>> result, List<Integer> current, int[] nums, int start) {
19 result.add(new ArrayList<>(current));
20
21 for (int i = start; i < nums.length; i++) {
22 // Skip duplicates
23 if (i > start && nums[i] == nums[i - 1]) {
24 continue;
25 }
26 current.add(nums[i]);
27 backtrack(result, current, nums, i + 1);
28 current.remove(current.size() - 1);
29 }
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected