JSFiddle - React, Tailwind, and code Playground
JavaScript
var findLatestWeight = function(weights, i = weights.length - 1) {
const cur = weights.length - 1 === i;
if (i === 0) return weights[0];
weights.sort((a, b) => a - b);
console.log( weights );
weights[i - 1] = (weights[i] === weights[i-1]) ? 0 : weights[i] - weights[i-1];
return findLatestWeight(weights, i - 1);
}
console.log( findLatestWeight( [ 5, 3, 9, 4, 7, 1, 2, 6 ] ) );