Skip to content

Commit 7d31613

Browse files
authored
Merge pull request #5 from marcode24/2023-01
📄 add LICENSE
2 parents 6e8c87a + abe1dd2 commit 7d31613

File tree

6 files changed

+165
-3
lines changed

6 files changed

+165
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Reto 1: !Primer regalo repetido!
2+
3+
## Problema
4+
5+
En la fábrica de juguetes del Polo Norte, cada juguete tiene un número de identificación único.
6+
7+
Sin embargo, debido a un error en la máquina de juguetes, algunos números se han asignado a más de un juguete.
8+
9+
¡Encuentra el primer número de identificación que se ha repetido, **donde la segunda ocurrencia tenga el índice más pequeño!**
10+
11+
En otras palabras, si hay más de un número repetido, debes devolver el número cuya segunda ocurrencia aparezca primero en la lista. Si no hay números repetidos, devuelve -1.
12+
13+
```js
14+
const giftIds = [2, 1, 3, 5, 3, 2]
15+
const firstRepeatedId = findFirstRepeated(giftIds)
16+
console.log(firstRepeatedId) // 3
17+
// Aunque el 2 y el 3 se repiten
18+
// el 3 aparece primero por segunda vez
19+
20+
const giftIds2 = [1, 2, 3, 4]
21+
const firstRepeatedId2 = findFirstRepeated(giftIds2)
22+
console.log(firstRepeatedId2) // -1
23+
// Es -1 ya que no se repite ningún número
24+
25+
const giftIds3 = [5, 1, 5, 1]
26+
const firstRepeatedId3 = findFirstRepeated(giftIds3)
27+
console.log(firstRepeatedId3) // 5
28+
```
29+
30+
**¡Ojo!** Los elfos dicen que esto es una prueba técnica de Google.
31+
32+
## Mi solución
33+
34+
```js
35+
const findFirstRepeated = (gifts) => gifts.find((gift, index) => gifts.indexOf(gift) !== index) ?? -1;
36+
```
37+
38+
## Explicación de mi solución
39+
40+
1. Utilizo el método `find` para encontrar el primer elemento que cumpla la condición de que el índice del elemento en el array sea diferente al índice del elemento en el array si se utiliza el método `indexOf` para encontrar el índice del elemento en el array. Si no se encuentra ningún elemento que cumpla la condición, se devuelve `undefined`.
41+
42+
2. Si el resultado de la expresión anterior es `undefined`, se devuelve `-1` utilizando el operador de encadenamiento opcional `??`.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// complejidad: 1
2+
const findFirstRepeated = (gifts) => gifts.find((gift, index) => gifts.indexOf(gift) !== index) ?? -1;
3+
4+
// complejidad: 3
5+
const findFirstRepeated2 = (gifts) => {
6+
const repeated = gifts.filter((id, index) => gifts.indexOf(id) !== index);
7+
return repeated.length > 0 ? repeated[0] : -1;
8+
};
9+
10+
// complejidad: 3
11+
const findFirstRepeated3 = (gifts) => (
12+
[...new Set(gifts)].length === gifts.length
13+
? -1
14+
: gifts.find((id, index) => gifts.indexOf(id) !== index)
15+
);
16+
17+
module.exports = {
18+
findFirstRepeated,
19+
findFirstRepeated2,
20+
findFirstRepeated3,
21+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { findFirstRepeated, findFirstRepeated2, findFirstRepeated3 } = require('./index');
2+
3+
describe('01 => Primer regalo repetido', () => {
4+
const testCases = [
5+
{
6+
input: [2, 1, 3, 5, 3, 2],
7+
output: 3,
8+
},
9+
{
10+
input: [2, 2],
11+
output: 2,
12+
},
13+
{
14+
input: [2, 4, 3, 5, 1],
15+
output: -1,
16+
},
17+
{
18+
input: [1],
19+
output: -1,
20+
},
21+
{
22+
input: [],
23+
output: -1,
24+
},
25+
{
26+
input: [10, 20, 30],
27+
output: -1,
28+
},
29+
{
30+
input: [1, 2, 3, 4],
31+
output: -1,
32+
},
33+
{
34+
input: [5, 1, 2, 3, 2, 5, 1],
35+
output: 2,
36+
},
37+
{
38+
input: [12, 20, 30, 11, 20, 12],
39+
output: 20,
40+
},
41+
{
42+
input: [5, 1, 5, 1],
43+
output: 5,
44+
},
45+
];
46+
47+
it('should return a number type', () => {
48+
expect(typeof findFirstRepeated([])).toBe('number');
49+
expect(typeof findFirstRepeated2([])).toBe('number');
50+
expect(typeof findFirstRepeated3([])).toBe('number');
51+
});
52+
53+
it.each(testCases)('should return $output', ({ input, output }) => {
54+
expect(findFirstRepeated(input)).toBe(output);
55+
expect(findFirstRepeated2(input)).toBe(output);
56+
expect(findFirstRepeated3(input)).toBe(output);
57+
});
58+
});

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Marco Antonio Cruz Gabino [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ This is a repository with my solutions to the [AdventJS](https://adventjs.dev/)
66

77
Built by [@midudev](https://twitter.com/midudev)
88

9+
## 🎯 2023 Challenges
10+
11+
<details>
12+
<summary show>Show / Hide</summary>
13+
14+
| # | Challenge | Difficulty | My Solution |
15+
| :-: | ------------------------------------------------------------------------------------------- | :--------: | :------------------------------------------------------------------------------------------------: |
16+
| 01 | [¡Primer regalo repetido!](https://adventjs.dev/es/challenges/2023/1) | 🟢 | [here](https://github.com/marcode24/adventjs-solutions/tree/main/2023/01-primer-regalo-repetido) |
17+
18+
Difficulties legend:
19+
🟢 Easy 🟡 Medium 🔴 Hard
20+
21+
</details>
22+
923
## 🎯 2022 Challenges
1024

1125
<details hide>
@@ -68,6 +82,8 @@ npm run test
6882
npm run test:2021
6983
# or
7084
npm run test:2022
85+
# or
86+
npm run test:2023
7187

7288
# Run specific test
7389
npm run test 'year'/'challenge'/index.test.js

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "adventjs-solutions",
33
"version": "1.0.0",
4-
"description": "",
4+
"description": "Solutions for AdventJS 2022",
55
"main": "index.js",
66
"scripts": {
77
"test": "jest",
88
"test:2021": "jest --testPathPattern=2021",
99
"test:2022": "jest --testPathPattern=2022",
10+
"test:2023": "jest --testPathPattern=2023",
1011
"lint": "eslint **/*.js",
1112
"lint:fix": "eslint **/*.js --fix",
1213
"prepare": "husky install"
@@ -17,10 +18,13 @@
1718
"adventjs",
1819
"adventjs-2022",
1920
"adventjs-2022-solutions",
20-
"adventjs-2022-solutions-javascript"
21+
"adventjs-2022-solutions-javascript",
22+
"adventjs-2023",
23+
"adventjs-2023-solutions",
24+
"adventjs-2023-solutions-javascript"
2125
],
2226
"author": "marcode24",
23-
"license": "ISC",
27+
"license": "MIT",
2428
"devDependencies": {
2529
"eslint": "^8.30.0",
2630
"eslint-config-airbnb-base": "^15.0.0",

0 commit comments

Comments
 (0)