# Why does this heap-based merge skip some values?

**URL:** <https://forum.kirupa.com/t/why-does-this-heap-based-merge-skip-some-values/680131>\
**Category:** web dev\
**Created:** [April 7, 2026, 4:00am UTC](https://forum.kirupa.com/t/why-does-this-heap-based-merge-skip-some-values/680131 "2026-04-07T04:00:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 7, 2026, 4:00am UTC](https://forum.kirupa.com/t/why-does-this-heap-based-merge-skip-some-values/680131/1 "2026-04-07T04:00:12Z")

</div>

Hey everyone, I’m working on a small utility to merge several sorted arrays, and I’m using a min-heap so I don’t have to flatten everything first. It mostly works, but on duplicate values I sometimes lose items, which is worse than being a little slower.

```js
function mergeSorted(lists) {
  const heap = [];
  const out = [];

  function push(node) {
    heap.push(node);
    heap.sort((a, b) => a.value - b.value);
  }

  for (let i = 0; i < lists.length; i++) {
    if (lists[i].length) {
      push({ value: lists[i][0], list: i, index: 0 });
    }
  }

  while (heap.length) {
    const cur = heap.shift();
    out.push(cur.value);

    const nextIndex = cur.index + 1;
    if (lists[cur.list][nextIndex]) {
      push({ value: lists[cur.list][nextIndex], list: cur.list, index: nextIndex });
    }
  }

  return out;
}

console.log(mergeSorted([[1, 4, 4], [1, 3], [2, 4]]));

```

What am I overlooking here that makes some valid next values disappear when merging sorted arrays?

Hari 😎

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 7, 2026, 4:21am UTC](https://forum.kirupa.com/t/why-does-this-heap-based-merge-skip-some-values/680131/2 "2026-04-07T04:21:28Z")

</div>

You’re checking the next item by truthiness, not by whether that index exists.

So `if (lists[cur.list][nextIndex])` will drop real values like `0` or `""`. The duplicate `4`s aren’t the issue here. Change it to:

```auto

js
if (nextIndex < lists[cur.list].length) {
  push({ value: lists[cur.list][nextIndex], list: cur.list, index: nextIndex });
}

```

For your sample with `[[1, 4, 4], [1, 3], [2, 4]]`, that should give `[1, 1, 2, 3, 4, 4, 4]`.

Yoshiii
