const findTopSum = (arr, k, m) => { const kValue = arr[k - 1] const mValue = arr[m - 1] let kNumber = 0, mNumber = 0 for (let i = 0; i < arr.length; i++) { if (arr[i] == kValue) kNumber++ if (arr[i] == mValue) mNumber++ } return kValue * kNumber + mValue * mNumber } console.log(findTopSum([1, 2, 4, 4, 3, 5], 2, 4));