Advertisement
rishu110067

Untitled

Jan 24th, 2022
1,066
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const subsetsWithDup = (arr) => {
  2.     // sorting makes sure we have the most unique vectors
  3.     // i.e. [2,1,2] === [1,2,2] (same)
  4.     arr = arr.sort();
  5.     return helper(arr);
  6. }
  7.  
  8. const helper = (arr, index = 0, slate = [], results = [], set = new Set()) => {
  9.     results.push(slate);
  10.  
  11.     if (index === arr.length)
  12.         return;
  13.  
  14.     for (let i = index; i < arr.length; i++) {
  15.         if(i != index && arr[i] == arr[i-1])
  16.             continue;
  17.         helper(arr, i + 1, [...slate, arr[i]], results, set);
  18.     }
  19.  
  20.     return results;
  21. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement