Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Calculate highest occupancy over time period

Hi im doing a job at work where we have a camera system that pings everytime someone enters or exits a room so you would have data like this:

{enter:7, exit:6}

which should mean one person is left in room

I want to know what was the highest amount of people in the room over a given period lets just say 45 minutes. I could have 50 to 100 points of data how can i would out what the average would be

Thanks

// update with a code sample here

.then((results) => {
  // maybe a correct algorithim

  results.sort((a, b) => b.enter - a.enter);

  results.sort((a, b) => b.exit - a.exit);

  results.sort((a, b) => a.enter - b.enter || b.exit - a.exit);

  // results.sort(fieldSorter(["enter"]));
  let final_result = result.pop();

  console.log(final_result);
  if (final_result) {
    return parseInt(final_result.enter - final_result.exit);
  } else {
    return 0;
  }
})

So before this code im gathering all the datapoints then try and sort them and pop off the highest entertence to the lowest exits but i think im going about this wrong

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assuming that enter time is always smaller than exit time (which contradicts your example data), the issue could be solved like this:

function getAverage(startTime, endTime, items) {
    let slots = {};
    for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
        slots[timeIndex] = 0;
    }
    for (item of items) {
        let enter = Math.min(item.enter, startTime);
        let exit = Math.min(item.exit, endTime);
        for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
            slots[momentIndex]++;
        }
    }
    let avg = 0;
    for (let slot of slots) avg += slot;
    return avg / slots.length;
}

EDIT

For computing the maximum you can do something like this:

function getAverage(startTime, endTime, items) {
    let slots = {};
    for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
        slots[timeIndex] = 0;
    }
    for (item of items) {
        let enter = Math.min(item.enter, startTime);
        let exit = Math.min(item.exit, endTime);
        for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
            slots[momentIndex]++;
        }
    }
    let max = 0;
    for (let slot of slots) max = Math.max(max, slot);
    return max;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!