【问题标题】:Understanding Finding Duplicate Numbers And Returning The Lowest Index of The Second Occurence了解查找重复数字并返回第二次出现的最低索引
【发布时间】:2017-07-16 16:52:28
【问题描述】:

关于在数组中查找第二次出现的具有最小索引的数字的第一个重复数字的问题。

到目前为止,如果我理解的话,变量 firstDuplicate 是一个函数对象,它使用箭头符号来缩写“var firstDuplicate = function (a){}”等。这是我的问题开始的地方。

1) 创建一个新的集合对象会自动使用传递函数的数组来填充? set 方法如何知道将数组传递给函数并对其进行设置?

2) 现在我在 for 循环中了解到数组中的每个项目都在被遍历并且当前索引是 e,但是在这里我开始从概念上迷失正在发生的事情。在以下位置:

if (r.has(e))

比较到底发生在哪里,即检查该数组中的重复数字是什么,以及确定第二次重复出现的最低索引是多少的比较?

const test1 =  [0, 3, 4, 10, 2, 4, 2, 3]
  firstDuplicate = a => {
    r = new Set()
             for (e of a)
        if (r.has(e))
          return e
      else
        r.add(e)
return -1
}

console.log(firstDuplicate(test1));

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    创建一个新的集合对象会使用传递函数的数组自动填充? set 方法如何知道将传递给函数的数组取为一组?

    这里没有任何事情是自动发生的。当您说r.add(e) 时,Set 会填充到else 条件中

    现在我在 for 循环中了解到数组中的每个项目都在被遍历并且当前索引是 e...

    e 是当前元素而不是当前索引。您使用的是for..of 语句而不是for..in 语句。两者的区别用一个简单的sn-p

    const a = [10, 5, 12];
    
    console.log("for..in statement");
    for (const i in a) {
      // Here i is the index
      console.log(i)
    };
    
    console.log("for..of statement");
    for (const i of a) {
      // Here i is the element
      console.log(i)
    };

    ...比较到底发生在哪里,即检查该数组中的重复数字是什么,以及确定第二次重复出现的最低索引是多少的比较?

    比较发生在r.has(e)。因此代码检查是否已经遇到e(因此也添加到Set 中)并返回e。这是有效的,因为如果 e 已经遇到过一次,那么如果在任何其他重复项之前再次遇到它,则自动意味着 e 处于最小索引处。

    这是您的代码的注释版本,以使其更清晰

    const test1 = [2, 3, 6, 10, 2, 3, 7, 9, 1]
    
    firstDuplicate = a => {
      // Create an empty Set
      r = new Set()
    
      // Iterate over every element of a
      for (e of a) {
        // Check if e was already encountered
        if (r.has(e)) {
          // If e was already encountered, this is the first time a duplicate
          // has been found. This is definitely a duplicate at the minimal index,
          // as a for loop iterates elements in order of their indices
          return e
        } else { // e is encountered the firt time
          // Populate the Set with e, to say that it has been encountered atleast
          // once
          r.add(e)
        }
      }
      // The if condition above never passed. That means array a has all
      // unique elements. Return -1, assuming the array a will never contain
      // -1 as one of it's elements.
      return -1
    }
    
    console.log(firstDuplicate(test1));
    

    【讨论】:

    • 感谢 Maaz 抽出宝贵时间。我正在检查您的解决方案,可能会提出有关它的问题。谢谢。
    • 所以我只是更改了我的原始代码,我只是用我看到的数据替换了具有不同值的数组。 [0, 3, 4, 10, 2, 4, 2, 3] 如您所见,有三个重复的数字。这将返回 4。作为 4 第二次出现的索引,小于 2 的第二次出现和 3 的第二次出现。我的问题——对于每个 e 的第一遍,由于 r 尚未初始化,它 r 是空的,所以对于第一遍,控制将始终直接流向 else 子句,并填充 r。是对的吗? 'if (r.has(e))' 不应该总是返回 true 吗?
    • 我不明白代码在哪里比较重复数字的索引以找出第二次出现的索引最小的位置。
    • 只有一张。因此,只有在集合中找不到元素时,流程才会进入else。否则,它将进入if 并返回该元素。我试图在上面评论的 sn-p 本身中解释它。此外,如上所述,不需要进行任何索引比较。该代码只返回它找到的第一个重复项(自动成为最小索引)。考虑尝试使用多个混乱的卡片组来解决这个问题。一旦找到重复的卡片,你会费心去翻看其他卡片吗?
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多