-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #1
- Loading branch information
Showing
4 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters