最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find the lowest unused number in an array using Javascript - Stack Overflow

programmeradmin4浏览0评论

I have an array full of numbers. Here's an example:

myArray = [0,1,2,4,5];

I need to find the lowest unused number starting from 1, so in this case it will be 3.

I've been reading up of using indexOf but I'm unsure how to use it for my specific purpose.

I have an array full of numbers. Here's an example:

myArray = [0,1,2,4,5];

I need to find the lowest unused number starting from 1, so in this case it will be 3.

I've been reading up of using indexOf but I'm unsure how to use it for my specific purpose.

Share Improve this question edited Jun 5, 2015 at 17:54 Oleksandr T. 77.5k17 gold badges176 silver badges145 bronze badges asked Jun 5, 2015 at 17:53 Andrew HowardAndrew Howard 3,0926 gold badges40 silver badges71 bronze badges 7
  • 1 Is the array guaranteed to be sorted, as in your example? – Ed Bayiates Commented Jun 5, 2015 at 17:55
  • 1 iter on your array. find the first item that does not match the index. – njzk2 Commented Jun 5, 2015 at 17:56
  • @njzk2 this only works if the array is always sorted. – abc123 Commented Jun 5, 2015 at 17:57
  • I would reconsider your design if this is required – Ed Heal Commented Jun 5, 2015 at 17:57
  • 2 Can the array start with any number like [23,24,26,27] and it will be 25 in this case? – meteor Commented Jun 5, 2015 at 17:59
 |  Show 2 more ments

2 Answers 2

Reset to default 5

Assuming the array isn't sorted, you always start at 0, and taking into account your desire to find a highest number if there isn't one missing:

var k = [6, 0, 1, 2, 4, 5];

k.sort(function(a, b) { return a-b; });   // To sort by numeric

var lowest = -1;
for (i = 0;  i < k.length;  ++i) {
  if (k[i] != i) {
    lowest = i;
    break;
  }
}
if (lowest == -1) {
    lowest = k[k.length - 1] + 1;
}
console.log("Lowest = " + lowest);

Logs answer 3. If 3 was also in there, would log 7 since no other number is missing.

If you aren't always starting at zero, use an offset:

var k = [6, 2, 3, 4, 5];

k.sort(function(a, b) { return a-b; });   // To sort by numeric

var offset = k[0];
var lowest = -1;
for (i = 0;  i < k.length;  ++i) {
  if (k[i] != offset) {
    lowest = offset;
    break;
  }
  ++offset;
}
if (lowest == -1) {
    lowest = k[k.length - 1] + 1;
}
console.log("Lowest = " + lowest);

Logs answer 7 since none are missing after 2 which starts the sequence.

This takes a sequence starting from a number (like 1 in your example) and returns the lowest unused number in the sequence.

function lowestUnusedNumber(sequence, startingFrom) {
  const arr = sequence.slice(0);
  arr.sort((a, b) => a - b);

  return arr.reduce((lowest, num, i) => {
    const seqIndex = i + startingFrom;
    return num !== seqIndex && seqIndex < lowest ? seqIndex : lowest
  }, arr.length + startingFrom);
}

Example:

> lowestUnusedNumber([], 1)
1
> lowestUnusedNumber([1,2,4], 1)
3
> lowestUnusedNumber([3], 1)
1

In exchange for readability, it's slightly less optimized than the other example because it loops over all items in the array instead of breaking as soon as it finds the missing item.

发布评论

评论列表(0)

  1. 暂无评论