【问题标题】:Find the index number of the fourth occurrence in an array [duplicate]查找数组中第四次出现的索引号[重复]
【发布时间】:2021-08-13 17:32:57
【问题描述】:

提示是在数组中查找数字第四次出现的索引号。我正在尝试在 for 循环语句中实现 break,我不确定如何使其工作。 这是我的代码:

let array = [0,4,4,3,2,1,4,5,6,4,6,9];

for (i = 0; i <= array.length; i++){
    if (array[i] === 4){
        console.log("The fourth occurrence of 4 is:", i)
        break;
    }
}

假设输出

The fourth occurrence of 4 is: 9

【问题讨论】:

  • 问题/问题是? -> How do I ask a good question?
  • 数组是从零开始的。最后一个元素位于索引array.length - 1 而不是array.length

标签: javascript


【解决方案1】:

这将为您解决问题:

function getFourthOccurance(number){
    let ocurrences = 0;
    for (i = 0; i <= array.length; i++){
        if (array[i] === number){
            ocurrences++;
            if(ocurrences === 4){
                return i;
            }
        }
    }
}

let array = [0,4,4,3,2,1,4,5,6,4,6,9];
console.log("The fourth occurrence of 4 is:", getOccurance(4))

【讨论】:

  • 谢谢,这就是我一直在寻找的。澄清一下,为什么我们需要创建另一个变量名出现?
  • 你需要知道你给出的值被找到了多少次。在您的问题上, array[i] === 4 仅断言数组内 i 位置的值等于 4,而不是找到了多少次。如果对您有帮助,请随时为答案投票。
【解决方案2】:

你在找这个吗:

let array = [0,4,4,3,2,1,4,5,6,4,6,9];

let numberOfOccurencess = 0;
for (i = 0; i <= array.length; i++){
    if (array[i] === 4){
       numberOfOccurencess++;
    } 
   
   if(numberOfOccurencess === 4) {
      console.log('4th occurence is', i);
      break;
   }
}

【讨论】:

    【解决方案3】:

    请使用此代码。

    let array = [0,4,4,3,2,1,4,5,6,4,6,9];
    let count = 0;
    const num = 4;
    for (i = 0; i <= array.length; i++){
        if (array[i] === num){
            count++;
            if(count === 4) {
                console.log("The fourth occurrence of 4 is:", i)
                break;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      您应该将出现次数存储在某个变量中。

      let array = [0,4,4,3,2,1,4,5,6,4,6,9];
      var occurrences = 0;
      for (i = 0; i <= array.length; i++){
          if (array[i] === 4){
              occurrences += 1;
              if (occurrences === 4) {
                  console.log("The fourth occurrence of 4 is:", i)
                  break;
              }
          }
      }

      【讨论】:

        【解决方案5】:

        您可以从先前已知的索引迭代更新最后一个索引。

        const indexOfNthOccurance = (arr, value, occurance = 1) => {
          let lastIndex = -1;
          while (occurance > 0) {
            lastIndex = arr.indexOf(value, lastIndex + 1);
            occurance--;
          }
          return lastIndex;
        };
        
        const array = [0, 4, 4, 3, 2, 1, 4, 5, 6, 4, 6, 9, 0, 1];
        
        console.log(`The 4th occurrence of 4 is: ${indexOfNthOccurance(array, 4, 4)}`); //  9
        console.log(`The 2nd occurrence of 0 is: ${indexOfNthOccurance(array, 0, 2)}`); // 12

        【讨论】:

          【解决方案6】:

          你可以使用地图/过滤器。

          let array = [0, 4, 4, 3, 2, 1, 4, 5, 6, 4, 6, 9];
          
          const indices = array.map((e, i) => (e === 4 ? i : -1)).filter(i => i !== -1);
          
          console.log(`The fourth occurrence of 4 is at index ${indices[3]}.`);

          【讨论】:

            【解决方案7】:

            function nthInstanceFinder(arr, num, n){ 
              let counter = 0, target = n 
              let i = 0 
              if (target === 0 || arr.length === 0 || num === undefined) 
                console.log("check your input and try again") 
              while (counter < target && i < arr.length) {  
                if (arr[i]===num)
                  counter +=1 
                i++ 
              } 
                if (counter===target) 
                  console.log(target, " occurance of ", num, " is at index ", i-1) 
                 else console.log( "Array does not contain ", target, " instance(s) of ", num) 
            } 
              
            let arr=[0,4,4,3,2,1,4,5,6,4,6,9]
            let numberToFind=4 
            let n=3 
            nthInstanceFinder(arr, numberToFind, n)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-12-20
              • 1970-01-01
              • 1970-01-01
              • 2018-05-21
              • 2021-01-20
              • 1970-01-01
              • 2018-06-20
              • 1970-01-01
              相关资源
              最近更新 更多