diff --git a/Problems/Missing-Number/README.md b/Problems/Missing-Number/README.md new file mode 100644 index 0000000..ec7efb9 --- /dev/null +++ b/Problems/Missing-Number/README.md @@ -0,0 +1,27 @@ +# Missing-Number + +Create a function that takes an array of numbers between 1 and 10 (excluding one number) and returns the missing number. + +## Infos which are maybe needed + +- The array of numbers will be unsorted (not in order). +- Only one number will be missing. + +## Documentation + +### Solution Idea + +Sum all numbers in the array together and subtract this number from 55 (The sum from 1 to 10). + +--- + +### [Implementation](./solver.ts) + +Doing exactly this what the solution idea says. + +--- + +### Information's + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/jit-compliation) from February 11, 2026. diff --git a/Problems/Missing-Number/missing-number.test.ts b/Problems/Missing-Number/missing-number.test.ts new file mode 100644 index 0000000..5ba8605 --- /dev/null +++ b/Problems/Missing-Number/missing-number.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import missingNumber from "./solver"; + +describe("Finds the missing number", () => { + it("should return 5 for the first number-row", () => { + expect(missingNumber([1, 2, 3, 4, 6, 7, 8, 9, 10])).toEqual(5); + }); + + it("should return 10 for the second number-row", () => { + expect(missingNumber([7, 2, 3, 6, 5, 9, 1, 4, 8])).toEqual(10); + }); + + it("should return 7 for the third number-row", () => { + expect(missingNumber([10, 5, 1, 2, 4, 6, 8, 3, 9])).toEqual(7); + }); +}); diff --git a/Problems/Missing-Number/solver.ts b/Problems/Missing-Number/solver.ts new file mode 100644 index 0000000..26280f8 --- /dev/null +++ b/Problems/Missing-Number/solver.ts @@ -0,0 +1,6 @@ +import type { FixedArray } from "../../utilities/types/fixedArray"; +import type { IntRange } from "../../utilities/types/intRange"; + +export default function missingNumber(array: FixedArray, 9>): number { + return 55 - array.reduce((preVal, val) => preVal + val, 0); +} diff --git a/README.md b/README.md index 4386135..7d58c23 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha ## Problems -Solved: **24 problems** +Solved: **25 problems** -🟢 Easy: 18 +🟢 Easy: 19 🟠 Medium: 5 🔴 Hard: 1 @@ -40,6 +40,7 @@ Solved: **24 problems** | [Day number of year](./Problems/Day-Number-Of-Year/README.md) | 🟢 Easy | | [Remove the last vowel](./Problems/Remove-The-Last-Vowel/README.md) | 🟢 Easy | | [Bingo Check](./Problems/Bingo-Check/README.md) | 🟢 Easy | +| [Missing Number](./Problems/Missing-Number/README.md) | 🟢 Easy | diff --git a/utilities/types/intRange.ts b/utilities/types/intRange.ts new file mode 100644 index 0000000..ed5b8df --- /dev/null +++ b/utilities/types/intRange.ts @@ -0,0 +1,9 @@ +// Source - https://stackoverflow.com/a/39495173 +// Posted by AlexG, modified by community. See post 'Timeline' for change history +// Retrieved 2026-03-14, License - CC BY-SA 4.0 + +type Enumerate = Acc["length"] extends N + ? Acc[number] + : Enumerate; + +export type IntRange = Exclude, Enumerate>;