Skip to content

Commit

Permalink
feat(2023): add solution for challenge #1
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 1, 2023
1 parent a3744eb commit cab901c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
28 changes: 28 additions & 0 deletions 2023/challenge-01/README.md
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions 2023/challenge-01/challenge-01.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe } from 'vitest'
import { findFirstRepeated } from './challenge-01'

const TEST_CASES: TestCases<number[], number> = [
{
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,
})
})
7 changes: 7 additions & 0 deletions 2023/challenge-01/challenge-01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function findFirstRepeated(gifts: number[]) {
const firstRepeatedGift = gifts.find((gift, index) => {
return gifts.indexOf(gift) !== index
})

return firstRepeatedGift ?? -1
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |

</details>

Expand Down

0 comments on commit cab901c

Please sign in to comment.