2 Sam 21:19 and 1 Chron 20:5 has Elhanan, not David, as the one who killed Goliath. There have been various proposals for this issue. One is that David and Elhanan are the same person. According to one defender of this thesis, it is
reasonable
to adopt the solution already offered by ancient commentators and modern
scholars who have suggested identifying Elhanan with David. We suggest reconstructing
the process as follows: when Jesse’s eighth son was born, he called the new
infant Elhanan, namely, “God bestowed me” (האל חנני) with another son (cf. Gen
33:5). This youngest son, Elhanan, killed Goliath, and gradually became an
admired high-ranking commander in Saul’s army. This popular officer at some
point is granted by his admirers a nickname, “David”, which means “beloved one”.
Indeed, the author of the first version of Samuel plays on the synonymous word אה"ב
(to love—cf. Song 3:1-4) by stating that everybody “loved” David: Saul (1 Sam
16:21), Jonathan (18:1-3), Michal (ibid vs. 20, 28), Saul’s officers (ibid v.
22), and all Israel and Judah (ibid v. 16). These are examples of a Midrashic
name derivation playing in David’s nickname. The first author of Samuel
continued to use this wordplay on Solomon’s nickname, Jedediah, that is
interpreted by its synonymous verb אה"ב: “The Lord loved him”
(2 Sam 12:24-25). At a later stage, the nickname “David” turned into his regal
name . . .(Moshe Garsiel, The Book of Samuel: Studies in History,
Historiography, Theology and Poetics Combined, Part one—The Story and History
of David and His Kingdom [Jerusalem: Rubin Mass Ltd., 2018], 363)