Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找出现奇数次的元素_Javascript_Arrays_For Loop_Integer - Fatal编程技术网

Javascript 查找出现奇数次的元素

Javascript 查找出现奇数次的元素,javascript,arrays,for-loop,integer,Javascript,Arrays,For Loop,Integer,我试图解决这个问题,找到数组中出现奇数次的数字。到目前为止我已经知道了,但是输出结果是一个整数,它出现了偶数次。例如,数字2显示3次,数字4显示6次,但输出为4,因为它将其计为显示5次。它怎么会返回它找到的第一个奇数集呢?感谢您的帮助 function oddInt(array) { var count = 0; var element = 0; for(var i = 0; i < array.length; i++

我试图解决这个问题,找到数组中出现奇数次的数字。到目前为止我已经知道了,但是输出结果是一个整数,它出现了偶数次。例如,数字2显示3次,数字4显示6次,但输出为4,因为它将其计为显示5次。它怎么会返回它找到的第一个奇数集呢?感谢您的帮助

         function oddInt(array) {
         var count = 0;
         var element = 0;
         for(var i = 0; i < array.length; i++) {
           var tempInt = array[i];
           var tempCount = 0;
             for(var j = 0; j <array.length; j++) {
                if(array[j]===tempInt) {
                tempCount++;
                  if(tempCount % 2 !== 0 && tempCount > count) {
                  count = tempCount; 
                  element = array[j];
                }
               }
              }
             }
           return element;
           }
           oddInt([1,2,2,2,4,4,4,4,4,4,5,5]);
函数oddit(数组){
var计数=0;
var元素=0;
对于(var i=0;i
这是因为当选中第5个
4
时,您的条件
if(tempCount%2!==0&&tempCount>count)
为真。这将更新
计数
元素
变量

当选中第6个
4
时,条件为false

若要修复,请将条件移到最内层循环之外,以便仅在对数组中的所有数字进行计数后才对其进行检查。

函数oddit(数组){
//首先:让我们计算数组中所有元素的出现次数
var hash={};//对象用作数组中所有项的计数器(项将是键,计数将是值)
forEach(函数(e){//对于数组中的每个项e
if(hash[e])hash[e]++;//如果我们已经遇到了这个项,那么递增计数器
else散列[e]=1;//否则启动一个新计数器(用1初始化)
});
//第二:我们只选择出现奇数次的数字
var result=[];//结果数组
for(hash中的var e){//对于hash中的每个键e(键是数组的项)
if(hash[e]%2)//如果该项的计数是奇数
result.push(+e);//然后将项推入结果数组(因为它们是键,所以我们必须使用一元+将它们转换为数字)
}
返回结果;
}
log(oddit([1,2,2,4,4,4,4,5,5])
函数oddit(数组、minCount、returnOne){
minCount=minCount | | 1;
var itemCount=array.reduce(函数(a,b){
a[b]=(a[b]| | 0)+1;
返回a;
}, {});
/*
项目计数:{
"1": 1,
"2": 3,
"4": 6,
"5": 2,
"7": 3
}
*/
var values=Object.keys(itemCount).filter(函数(k){
返回itemCount[k]%2!==0&&itemCount[k]>=minCount;
});
returnOne?值[0]:值;
}
var输入=[1,2,2,4,4,4,4,4,5,5,7,7];
log(oddit(输入,3,true));
log(oddit(输入,1,true));

log(oddit(输入,2,false))
之所以发生这种情况,是因为每次它找到奇数时都要设置
元素
变量,所以当它找到一个、三个和五个
4
时就要设置它

让我们一步一步地检查代码:

function oddInt(array) {
    // Set the variables. The count and the element, that is going to be the output
    var count = 0;
    var element = 0;

    // Start looking the array
    for(var i = 0; i < array.length; i++) {
        // Get the number to look for and restart the tempCount variable
        var tempInt = array[i];
        var tempCount = 0;
        console.log("");
        console.log(" * Looking for number", tempInt);
        // Start looking the array again for the number to look for
        for(var j = 0; j <array.length; j++) {
            // If the current number is the same as the one that we are looking for, sum it up
            console.log("Current number at position", j, "is", array[j]);
            if(array[j]===tempInt) {
                tempCount++;
                console.log("Number found. Current count is", tempCount);
                // Then, if currently there are an odd number of elements, save the number
                // Note that you are calling this altough you don't have looped throgh all the array, so the console will log 3 and 5 for the number '4'
                if(tempCount % 2 !== 0 && tempCount > count) {
                    console.log("Odd count found:", tempCount);
                    count = tempCount;
                    element = array[j];
                }
            }
        }
    }
    return element;
}
oddInt([1,2,2,2,4,4,4,4,4,4,5,5]);
函数oddit(数组){
//设置变量。计数和元素,这将是输出
var计数=0;
var元素=0;
//开始查看阵列
对于(var i=0;i
function findOdd(A) {
    var num;
    var count =0;
    for(i=0;i<A.length;i++){
       num = A[i]
       for(a=0;a,a<A.length;a++){
          if(A[a]==num){
          count++;
          }
     } if(count%2!=0){
          return num;
     }
   }
}
函数findOdd(A){
var-num;
var计数=0;
对于(i=0;i
函数oddOne(已排序){
设temp=sorted[0];
让计数=0;
对于(变量i=0;i
首先查找频率,然后查找哪些频率是奇数:

const data = [1,2,2,2,4,4,4,4,4,4,5,5]
const freq = data.reduce(
  (o, k) => ({ ...o, [k]: (o[k] || 0) + 1 }), 
  {})
const oddFreq = Object.keys(freq).filter(k => freq[k] % 2)

// => ["1", "2"]

var arr=[1,2,2,2,3,4,3,3,3,4,5,5,9,9,10]

var arr1=[]

for(设i=0;i
{
var计数=0;
对于(设j=0;j
函数findOdd(数字){
var计数=0;

对于(var i=0;i如果我们确定只有一个数字会出现奇数次,我们可以对这些数字进行异或运算,并在n次比较中找到奇数次出现的数字。如果两个位不同,则异或为1,否则为0。真值表如下所示

A   B   A^B
0   0    0
0   1    1
1   0    1
1   1    0
所以当我们对所有的数进行异或运算时,最终的数就是出现奇数次的数。 让我们用一个数字和XOR用相同的数字(出现两次)。结果将是0,因为所有的位都是相同的。现在让我们用相同的数字对结果进行异或运算。现在的结果将是该数字,因为上一个结果的所有位都是0,并且在结果中只设置相同数字的设置位。现在将其扩展为n个数字的数组,出现偶数次的数字将给出结果0。奇数应用程序在最终结果中,数字的出现将导致该数字的出现

func oddInt(numbers: [Int]) -> Int {
var result = 0
for aNumber in numbers {
  result = result ^ aNumber
}
return result
}

这里有一个带有O(N)或O(N*log(N))的解决方案。

函数findOdd(A){
变量计数={};
对于(变量i=0;i
函数oddit(数组){
设结果=0;
for(数组的let元素){
 {
    var count=0;

    for(let j=0;j<arr.length;j++)

     {
        if(arr[i]==arr[j])

        {

           count++;
        }
      }

     if(count%2 != 0 )

        {

           arr1.push(arr[i]);
         }
  }
A   B   A^B
0   0    0
0   1    1
1   0    1
1   1    0
func oddInt(numbers: [Int]) -> Int {
var result = 0
for aNumber in numbers {
  result = result ^ aNumber
}
return result
}
function findOdd(A) {
    var count = {};
    for (var i = 0; i < A.length; i++) {
        var num = A[i];
        if (count[num]) {
            count[num] = count[num] + 1;
        } else {
            count[num] = 1;
        }
    }
    var r = 0;
    for (var prop in count) {
        if (count[prop] % 2 != 0) {
            r = prop;
        }
    }
    return parseInt(r); // since object properies are strings
}
var oddNumberTimes = (arr) => {
  let hashMap = {};

  for (let i = 0; i < arr.length; i++) {
    hashMap[arr[i]] = hashMap[arr[i]] + 1 || 1;
  }

  for (let el in hashMap) {
    if (hashMap[el] % 2 !== 0) {
      return el;
    }
  }

  return -1;
};
 #using python
 a=array('i',[1,1,2,3,3])
 ans=0
 for i in a:
     ans^=i
 print('The element that occurs odd number of times:',ans)