头像

Algesthesiahunter


访客:107

离线:4个月前



Javascript 代码

/**
 * @param {number[]} nums
 * @return {number}
 */
var duplicateInArray = function(nums) {
  let o = {}
  let l = nums.length
  for (let i = 0; i < nums.length; i++) {
    const v = nums[i]
    if (v >= 0 && v <= l - 1) {
      if (o[v] === undefined) {
        o[v] = v
      } else {
        o.num = v
      }
    } else {
      return -1
    }
  }
  if (o.num === undefined) {
    return -1
  } else {
    return o.num
  }
}