使用Xor在数组中查找重复项

时间:2020-04-24 04:32:34

标签: javascript

我有以下创建的代码,但是对于如何在开发人员工具上的console.log中调用此代码有一个问题,以便查看结果。我是js新手...

var num = [5,3,1,2,3,5,1];

let duplicateValue = (num) => {
    let countVal = 0;
    for (let i = 0; i< num.lenght; i++){
         countVal = countVal ^ num[i];}
  return countVal; }

console.log(duplicateValue);返回undefined .....

2 个答案:

答案 0 :(得分:1)

您输入错误的长度(长度),并且未在控制台日志中调用方法

var num = [5,3,1,2,3,5,1];

let duplicateValue = (num) => {
    let countVal = 0;
    for (let i = 0; i< num.length; i++){
         countVal = countVal ^ num[i];}
  return countVal; 
 }
console.log(duplicateValue(num));

var num = [5,3,1,2,3,5,1];

let duplicateValue = (num) => {
    let countVal = 0;
    for (let i = 0; i< num.length; i++){
         countVal = countVal ^ num[i];}
  return countVal; 
 }
console.log(duplicateValue(num));

答案 1 :(得分:0)

您需要像这样进行控制台。您需要将数组作为参数传递。

console.log(duplicateValue([5,3,1,2,3,5,1]))

var num = [5,3,1,2,3,5,1];

let duplicateValue = (num) => {
    let countVal = 0;
    for (let i = 0; i< num.lenght; i++){
         countVal = countVal ^ num[i];}
  return countVal;
}

console.log(duplicateValue([5,3,1,2,3,5,1]));