2023-03-24

 24 Mar 2023

Return whether any Two Numbers in an Array add up to a Given Number

Taken from DailyCodingProblem:

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Solution

This is the answer I came up with:

// typescript

const solution = (numbers: Array<number>, k: number): boolean | any => {
  for (let num of numbers) {
    let rem = k - num
    return (numbers.includes(rem)) ? true : false
  }
}

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/MYewdgzgLgBBIBsCuUCW4YF4YAoxIFsAjAUwCcIAuGAQTLIEMBPAHn2PID4AaGAa2rtSZAJTUiIRCQZgYAHxgymWTjADeAWABQMGADMQZXAhKx2MEHphDyEEeu27dJ2GRIEs-GAFprhR05uUEhksniEwhAAdKhgwMgAJiQQOG4EIvYA-DBQZEgkMNR6DAgQJAEAvtpVWtqgkFJRCCAA5jgB8Mho4DgA2gCMAAy8-QCsvADMvADsALoj0yLa9gD0Kzl55VpAA

Webmentions & Comments

Copyright © Paramdeo Singh · Built with Jekyll in 🇬🇾 · All Rights Reserved

[ this node is permanently morphing last updated on 28 May 2023 ]

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems