简体   繁体   English

查找数组中每个数字的第一次出现

[英]finding first occurrence of each digit in an array

Taking each four digit number of an array in turn, return the number that you are on when all of the digits 0-9 have been discovered.依次取数组的每个四位数字,当发现所有数字 0-9 时,返回您所在的数字。 If not all of the digits can be found, return "Missing digits!"如果不能找到所有数字,则返回“缺少数字!”

ive tried to loop through then set a conditional if (i != i+1) push into new array this just gave me the array, its apperent my logic is wrong.我试图循环然后设置一个条件 if (i != i+1) push into new array 这只是给了我这个数组,显然我的逻辑是错误的。 could anyone help me out谁能帮帮我

for example calling this function with arr =findAllDigits([5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]) should return 5057例如用 arr =findAllDigits([5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]) 调用这个函数应该返回 5057

while calling arr=findAllDigits([4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]) should return "missing numbers"在调用 arr=findAllDigits([4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]) 时,应该返回“缺失的数字”

    function findAllDigits(arr) {
  newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] != arr[i + 1]) newArr.push(arr[i]);
    console.log(newArr);
  }
}

do i need to split because it is taking everything before the comma as one number?我是否需要拆分,因为它将逗号之前的所有内容都作为一个数字? then itterate over?然后迭代?

You can use Set here您可以在此处使用Set

Loop over the array and then create a set , You have to return the current number if set size becomes 10 because you need to check 0-9循环遍历array然后创建一个set ,如果set大小变为10 ,则必须返回当前数字,因为您需要检查0-9

 function findAllDigits(arr) { const set = new Set(); for (let n of arr) { String(n) .split("") .forEach((c) => set.add(c)); if (set.size === 10) return n; } return "Missing digits!"; } const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]; const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]; console.log(findAllDigits(arr1)); console.log(findAllDigits(arr2));

Your for loop is only checking to see if the array entry is equal to the next one.您的 for 循环只是检查数组条目是否等于下一个条目。 You need to split up the digits inside each entry and store them individually:您需要拆分每个条目中的数字并单独存储它们:

function findAllDigits(arr) {
  newArr = [];
  for (let i = 0; i < arr.length; i++) {
    // now iterate the individual digits
    const entryAsString = arr[i].toString();
    for (let j = 0; j < entryAsString.length; j++) {
      // if we haven't seen the digit before, add it to the array
      if(!newArr.includes(j) {
        newArr.push(j);
      }
    }
    // we know we have all digits when newArr is 10 entries long
    if (newArr.length) {
      console.log(arr[i]);
      // you can also return this value here
    }
  }
}

One more solution:另一种解决方案:

const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083];
const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868];
            
const findAllDigits = (arr) => {
  const digits = new Set();
  return arr.find((curr) => (
    [...String(curr)].forEach(digits.add, digits),
    (digits.size === 10)
  )) ?? "Missing digits!";
};
                
console.log(findAllDigits(arr1)); //5057
console.log(findAllDigits(arr2)); //Missing digits!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM