How to replace multiple items with same count of a different item?
You can summon the melon slice at the player's position in item form (with a small pickup delay) and then use /data to modify the Count property
To summon the item:
/execute at @a[nbt={SelectedItem:{id:"minecraft:villager_spawn_egg"}}] run summon item ~ ~ ~ {PickupDelay:10,Item:{id:"minecraft:melon_slice",Count:1,tag:{CustomModelData:10000101,display:{Name:"{\"text\":\"Blue Melon Slice\",\"italic\":\"false\"}"}}}}
to modify the Count property:
/execute as @a[nbt={SelectedItem:{id:"minecraft:villager_spawn_egg"}}] at @s store result entity @e[type=minecraft:item,limit=1,sort=nearest,distance=..0.2] Item.Count int 1 run scoreboard players get @s <objective_with_count_data>
and then clear the spawn egg from the player's inventory. There's the concept.
There is no "elegant" way to do this. The most direct way is using 64 almost identical commands, like this:
/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:1b}]}] run replaceitem entity @s hotbar.0 dirt 1
/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:2b}]}] run replaceitem entity @s hotbar.0 dirt 2
/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:3b}]}] run replaceitem entity @s hotbar.0 dirt 3
…
Alternatively you could use MegaCrafter10's answer of summoning an item entity that cannot be picked up for 1 tick and using /execute store
to modify its Item.Count
NBT field directly before the player picks it up, but that way the item lands in the first possible slot instead of the slot you want (and it has a delay of 1 tick).