2017-04-17 109 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"]