DEV Community

Cover image for Challenge 1 Solution AdventJs 2023 by Midudev.

Posted on

Challenge 1 Solution AdventJs 2023 by Midudev.

As is customary for the past couple of years, Miguel Ángel Durán García (midudev) has published the traditional AdventJs with programming challenges from December 1st to December 25th.

Honestly, I haven't been very active in past events, but I've set out to complete this challenge, and what better way to do it than by documenting it here.

Description of the First AdventJs 2023 Challenge

"In the toy factory at 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 should return the number whose second occurrence appears first in the list. If there are no repeated numbers, return -1."

const giftIds = [2, 1, 3, 5, 3, 2];
const firstRepeatedId = findFirstRepeated(giftIds);
console.log(firstRepeatedId); // 3
// Although 2 and 3 are repeated,
// 3 appears first for the second time

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
Enter fullscreen mode Exit fullscreen mode

My solution to this challenge

function findFirstRepeated(gifts) {
   /* The filter function checks if the current index of the element is different from the first index of occurrence of the element. */
  const repeatElements = gifts.filter((elem, index, arr) => arr.indexOf(elem) !== index );
  return repeatElements.length > 0 ? repeatElements[0] : -1;
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

The important part of this code is the constant repeatElements. I created a filter on the gifts array that essentially iterates through the array and applies the condition (arr.indexOf(elem) !== index) which adds the element if the index of the first occurrence of the current element (using the indexOf method) is different from the current index.

Finally, thanks to the ternary operator, if the length of the repeatElements array is greater than 0, we return the value of repeatElements at index 0 (which is precisely what we need in this case), and if not, it returns -1.

What Score Did I Get?

My score

My score was 400 initially (hence the screenshot), then I tried again and it changed to 270.

About the Use of Chat-GPT
Midudev clarified that the spirit of this challenge is to solve problems on our own to improve our problem-solving skills.

He also mentioned that if you get stuck, it's not wrong to ask the AI for a bit of help. After all, this is not a technical job test but rather a way to enhance our abilities as developers in a more entertaining manner than typical programming challenges.

Personally, I didn't use chat-GPT, but the idea of using the .filter() method along with indexOf() was inspired by a tutorial I had recently created on removing duplicate elements from an array. Link to the tutorial One of the methods is similar to the use case of this challenge.

If you've made it this far, I hope you've understood my explanation 😊😊 Thank you very much for reading. I invite you to participate in this challenge and share your achievements.

Top comments (0)