2017-04-17 107 views
1

我正在尝试解决此操作,查找在数组中出现奇数次的数字。到目前为止我有这个,但输出结果是一个偶数次的整数。例如,数字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]); 
+0

你想有一个是奇数或只有一个所有数字? –

+0

只有一个值!感谢您的回复 – padawan

+0

[计算JavaScript数组元素的出现次数](http://stackoverflow.com/questions/5667888/counting-the-occurrences-of-javascript-array-elements) –

回答

0

这是因为当第5 4检查您的病情if(tempCount % 2 !== 0 && tempCount > count)是真实的。这会更新countelement变量。

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

要修复,请移动最内层循环之外的条件,以便仅在数组中的所有数字都被计数后才检查该条件。

+0

感谢您的回应!你认为你可以展示如何格式化? – padawan

+0

你是什么意思格式化它? –

+0

感谢您的建议。豪尔赫冈萨雷斯详细阐述了它。欣赏它! – padawan

0

function oddInt(array) { 
 
    // first: let's count occurences of all the elements in the array 
 
    var hash = {};     // object to serve as counter for all the items in the array (the items will be the keys, the counts will be the values) 
 
    array.forEach(function(e) { // for each item e in the array 
 
    if(hash[e]) hash[e]++;  // if we already encountered this item, then increments the counter 
 
    else hash[e] = 1;   // otherwise start a new counter (initialized with 1) 
 
    }); 
 
    
 
    // second: we select only the numbers that occured an odd number of times 
 
    var result = [];    // the result array 
 
    for(var e in hash) {   // for each key e in the hash (the key are the items of the array) 
 
    if(hash[e] % 2)    // if the count of that item is an odd number 
 
     result.push(+e);   // then push the item into the result array (since they are keys are strings we have to cast them into numbers using unary +) 
 
    } 
 
    return result; 
 
} 
 
console.log(oddInt([1, 2, 2, 2, 4, 4, 4, 4, 4, 4, 5, 5]));

只返回第一个:

function oddInt(array) { 
 
    var hash = {}; 
 
    array.forEach(function(e) { 
 
    if(hash[e]) hash[e]++; 
 
    else hash[e] = 1; 
 
    }); 
 
    
 
    for(var e in hash) { // for each item e in the hash 
 
    if(hash[e] % 2) // if this number occured an odd number of times 
 
     return +e;  // return it and stop looking for others 
 
    } 
 
    // default return value here 
 
} 
 
console.log(oddInt([1, 2, 2, 2, 4, 4, 4, 4, 4, 4, 5, 5]));

0

function oddInt(array, minCount, returnOne) { 
 
    minCount = minCount || 1; 
 
    var itemCount = array.reduce(function(a, b) { 
 
    a[b] = (a[b] || 0) + 1; 
 
    return a; 
 
    }, {}); 
 
    /* 
 
    itemCount: { 
 
    "1": 1, 
 
    "2": 3, 
 
    "4": 6, 
 
    "5": 2, 
 
    "7": 3 
 
    } 
 
    */ 
 
    var values = Object.keys(itemCount).filter(function(k) { 
 
    return itemCount[k] % 2 !== 0 && itemCount[k]>=minCount; 
 
    }); 
 
    
 
    return returnOne?values[0]:values; 
 
} 
 

 
var input = [1, 2, 2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 7, 7, 7]; 
 

 
console.log(oddInt(input, 3, true)); 
 
console.log(oddInt(input, 1, true)); 
 
console.log(oddInt(input, 2, false));

0

那是因为你设置它找到的每个奇数时间element变量,所以要设置它,当它发现一个,三个和五个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]); 

我们想要做的是检查计数后循环所有的阵列,像这样:

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); 
      } 
     } 
     // After getting all the numbers, then we check the count 
     if(tempCount % 2 !== 0 && tempCount > count) { 
      console.log("Odd count found:", tempCount); 
      count = tempCount; 
      element = tempInt; 
     } 
    } 
    return element; 
} 
oddInt([1,2,2,2,4,4,4,4,4,4,5,5]); 

顺便说一句,这只是为了让你明白问题出在哪里并从中学习,尽管这不是最优化的方式,因为你可能已经注意到你正在寻找,比方说,编号为2三次,当你已经有了你第一次想要的输出。如果性能是作业的一部分,那么你应该想另一种方式:P

+0

非常感谢!真的帮了! – padawan

+0

@padawan很高兴!你可以接受或赞成答案。 –

0

“A”是要检查的数组。

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; 
    } 
    } 
} 
0
function oddOne (sorted) { 
    let temp = sorted[0]; 
    let count = 0; 
    for (var i = 0; i < sorted.length; i++) { 
    if (temp === sorted[i]) { 
     count++; 
     if (i === sorted.length - 1) { 
     return sorted[i]; 
     } 
    } else { 
     if (count % 2 !== 0) { 
     return temp; 
     } 

     count = 1; 
     temp = sorted[i]; 
    } 
    } 
} 
0

首先找到的频率,然后找出哪些是奇数:

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"]