DEV Community

DPC
DPC

Posted on

8 3 3 5 3

Daily JavaScript Challenge #JS-56: Find Missing Number in Arithmetic Sequence

Daily JavaScript Challenge: Find Missing Number in Arithmetic Sequence

Hey fellow developers! πŸ‘‹ Welcome to today's JavaScript coding challenge. Let's keep those programming skills sharp!

The Challenge

Difficulty: Medium

Topic: Numbers and Sequences

Description

Given an array that represents an arithmetic sequence with exactly one number missing, find the missing number in the sequence. An arithmetic sequence is a sequence of numbers with a constant difference between consecutive terms.

Ready to Begin?

https://www.dpcdev.com/

  1. Fork this challenge
  2. Write your solution
  3. Test it against the provided test cases
  4. Share your approach in the comments below!

Want to Learn More?

Check out the documentation about this topic here: https://en.wikipedia.org/wiki/Arithmetic_progression

Join the Discussion!

  • How did you approach this problem?
  • Did you find any interesting edge cases?
  • What was your biggest learning from this challenge?

Let's learn together! Drop your thoughts and questions in the comments below. πŸ‘‡


This is part of our Daily JavaScript Challenge series. Follow me for daily programming challenges and let's grow together! πŸš€

javascript #programming #coding #dailycodingchallenge #webdev

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhereβ€”across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

Top comments (5)

Collapse
 
raskinfe profile image
raskinfe β€’ β€’ Edited

Here is my solution for the challenge. Addressing more edge cases and finding multiple missing elements in the sequence. I have not tested it but I assume it should work fine :)

`const findMissingSequence = (arg) => {
if (!Array.isArray(arg)) {
throw new Error("Provided argument is not an array");
}
const length = arg.length;
if (length <= 1) {
return [];
}

const diffs = [];
for (let i = 0; i < length - 1; i++) {
    diffs.push(arg[i + 1] - arg[i]);
}

diffs.sort((a, b) => a - b)

const diff = diffs.shift();

if (diffs.some(d => d % diff !== 0)) {
    throw new Error('Provided argument is not an arithmetic sequence');
}

const missing = [];
let nextSequence = -Infinity;
const sequence = new Set(arg);
let i = 1;
while (true) {
    nextSequence = arg[0] + diff*(i - 1);
    if (nextSequence > arg[length - 1]) { 
      break;
    }

    if (!sequence.has(nextSequence)) {
        missing.push(nextSequence)
    }

    i++
}

return missing;
Enter fullscreen mode Exit fullscreen mode

}`

Collapse
 
dpc profile image
DPC β€’

It works!!

Thanks for sharing your code, we are waiting for you on today's challenge! You can try your code directly there!

Collapse
 
jay7134 profile image
Javed β€’

Its great daily challenge.

Collapse
 
dpc profile image
DPC β€’

See you in the next one! πŸ™Œ

Collapse
 
ctsl_db3e83f profile image
Chamod Theekshana β€’

Free programming codes here:
programmingforbegin.blogspot.com/

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

πŸ‘‹ Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's dayβ€”leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay