Simple ES6 solution


  • 0
    Y
    function subsets(array, start = 0, subsetsList = []) {
      if (start === array.length) {
        subsetsList.push([])
        return subsetsList
      }
      subsets(array, start + 1, subsetsList)
      const element = array[start]
      // need to store this in a variable, else length changes while mutating the array
      const currentLength = subsetsList.length
      for (let i = 0; i < currentLength; i++) {
        subsetsList.push(subsetsList[i].concat(element))
      }
      return subsetsList
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.