From cab901c2b4cda6161f720eb5d893a5e3b4ecc743 Mon Sep 17 00:00:00 2001 From: iswilljr Date: Fri, 1 Dec 2023 18:11:32 -0500 Subject: [PATCH] feat(2023): add solution for challenge #01 --- 2023/challenge-01/README.md | 28 ++++++++++++++++++++++++++ 2023/challenge-01/challenge-01.test.ts | 25 +++++++++++++++++++++++ 2023/challenge-01/challenge-01.ts | 7 +++++++ README.md | 5 +++-- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 2023/challenge-01/README.md create mode 100644 2023/challenge-01/challenge-01.test.ts create mode 100644 2023/challenge-01/challenge-01.ts diff --git a/2023/challenge-01/README.md b/2023/challenge-01/README.md new file mode 100644 index 0000000..ef6c97c --- /dev/null +++ b/2023/challenge-01/README.md @@ -0,0 +1,28 @@ +# Challenge #1: First gift repeated! + +In the toy factory of the North Pole, each toy has a unique identification number. + +However, due to an error in the toy machine, some numbers have been assigned to more than one toy. + +Find the first identification number that has been repeated, **where the second occurrence has the smallest index!** + +In other words, if there is more than one repeated number, you must return the number whose second occurrence appears first in the list. If there are no repeated numbers, return `-1`. + +```js +const giftIds = [2, 1, 3, 5, 3, 2] +const firstRepeatedId = findFirstRepeated(giftIds) +console.log(firstRepeatedId) // 3 +// Even though 2 and 3 are repeated +// 3 appears second time first + +const giftIds2 = [1, 2, 3, 4] +const firstRepeatedId2 = findFirstRepeated(giftIds2) +console.log(firstRepeatedId2) // -1 +// It is -1 since no number is repeated + +const giftIds3 = [5, 1, 5, 1] +const firstRepeatedId3 = findFirstRepeated(giftIds3) +console.log(firstRepeatedId3) // 5 +``` + +**Watch out!** The elves say this is a Google technical test. diff --git a/2023/challenge-01/challenge-01.test.ts b/2023/challenge-01/challenge-01.test.ts new file mode 100644 index 0000000..e927035 --- /dev/null +++ b/2023/challenge-01/challenge-01.test.ts @@ -0,0 +1,25 @@ +import { describe } from 'vitest' +import { findFirstRepeated } from './challenge-01' + +const TEST_CASES: TestCases = [ + { + args: [2, 1, 3, 5, 3, 2], + expected: 3, + }, + { args: [2, 2], expected: 2 }, + { args: [2, 4, 3, 5, 1], expected: -1 }, + { args: [1], expected: -1 }, + { args: [], expected: -1 }, + { args: [10, 20, 30], expected: -1 }, + { args: [5, 1, 2, 3, 2, 5, 1], expected: 2 }, + { args: [12, 20, 30, 11, 20, 12], expected: 20 }, + { args: [12, 10, 13, 12, 13], expected: 12 }, + { args: [0, 10, 13, 0, 13], expected: 0 }, +] + +describe('Challenge #1: First gift repeated!', () => { + buildChallengeTestCases({ + cases: TEST_CASES, + fn: findFirstRepeated, + }) +}) diff --git a/2023/challenge-01/challenge-01.ts b/2023/challenge-01/challenge-01.ts new file mode 100644 index 0000000..fbaa97d --- /dev/null +++ b/2023/challenge-01/challenge-01.ts @@ -0,0 +1,7 @@ +export function findFirstRepeated(gifts: number[]) { + const firstRepeatedGift = gifts.find((gift, index) => { + return gifts.indexOf(gift) !== index + }) + + return firstRepeatedGift ?? -1 +} diff --git a/README.md b/README.md index cea4c9b..64b30ea 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,9 @@ This repository contains the solutions to the challenges proposed by [@midudev]( ### 🕹️ Challenges -| # | Challenge | Difficulty[^1] | Description | Solution | -| :-: | :-------: | :------------: | :---------: | :------: | +| # | Challenge | Difficulty[^1] | Description | Solution | +| :-: | :------------------: | :------------: | :--------------------------------------------: | :---------------------------------------: | +| 01 | First gift repeated! | 🟢 | [Show](https://adventjs.dev/challenges/2023/1) | [Go](./2023/challenge-01/challenge-01.ts) |