Given a pivot x, and a list lst, partition the list into three parts The task:


  Given a pivot x, and a list lst, partition the list into three parts.
  
  The first part contains all elements in lst that are less than x The
  second part contains all elements in lst that are equal to x The third
  part contains all elements in lst that are larger than x Ordering
  within a part can be arbitrary.
  
  For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10], one
  partition may be `[9, 3, 5, 10, 10, 12, 14].


My solution:

const createPartitionOf = (lst, pivot) =&gt; {
  const lessLst = lst.filter(x =&gt; x &lt; pivot);
  const equalLst = lst.filter(x =&gt; x === pivot);
  const largerLst = lst.filter(x =&gt; x &gt; pivot);
  return [...lessLst, ...equalLst, ...largerLst];
};

console.log(createPartitionOf([9, 12, 3, 5, 14, 10, 10], 10));


My solution2:

const createPartitionOf2 = (lst, pivot) =&gt; {
  const lessLst = [];
  const equalLst = [];
  const largerLst = [];
  for (let i = 0; i &lt; lst.length; i++) {
    if (lst[i] &lt; pivot) {
      lessLst.push(lst[i]);
    } else if (lst[i] &gt; pivot) {
      largerLst.push(lst[i]);
    } else {
      equalLst.push(lst[i]);
    }
  }
  return [...lessLst, ...equalLst, ...largerLst];
};

console.log(createPartitionOf2([9, 12, 3, 5, 14, 10, 10], 10));
In your first solution you are traversing the list 3 times, whereas in the second solution only once. Given the functional-tag, I’d say you’re after the reduce function:

const flatten = (acc, x) =&gt; acc.concat(x)

const pivotPartition = (pivot, ary) =&gt; {
  const toOrdered = (triplet, n) =&gt; {
    let [less, equal, greater] = triplet

    switch(true) {
      case n &lt; pivot: less.push(n); break
      case n &gt; pivot: greater.push(n); break
      default: equal.push(n)
    }

    return triplet
  }

  return ary.reduce(toOrdered, [[], [], []]).reduce(flatten)
}


If you want to sort the subarrays as well, then the whole thing is just a ary.sort((a, b) =&gt; a - b), although as it is mutating the list, it isn’t a functional solution.