簡體   English   中英

如果其他數字是奇數,則返回“偶數”,而“奇數”其他數字是偶數 javascript

[英]return “even” if others numbers are odd and “odd” the others number are even javascript

我有 2 個問題,如何在數組中獲取值而不是值,以及如何使這段代碼更短且具有聲明性。

 arr = [16, 4, 11, 20, 2] arrP = [7, 4, 11, 3, 41] arrTest = [2, 4, 0, 100, 4, 7, 2602, 36] function findOutlier(arr) { const isPair = (num) => num % 2 === 0 countEven = 0 countOdd = 0 arr1 = [] arr2 = [] const result = arr.filter((ele, i) => { if (isPair(ele)) { countEven++ arr1.push(ele) } else { countOdd++ arr2.push(ele) } }) return countEven > countOdd? arr2: arr1 } console.log(findOutlier(arrTest))

過濾兩次可能更具可讀性。

even = arr.filter((x) => x % 2 == 0);
odd = arr.filter((x) => x % 2 == 1);
if (even.length > odd.length) {
    return even;
} else {
    return odd;
}

如果您希望使用一個循環來執行此操作,請考慮使用數組reduce方法將每個數字放入偶數或奇數桶中,然后比較返回中這些桶的長度:

 function findOutlier(arr) { const sorted = arr.reduce((acc, el) => { acc[el % 2].push(el); return acc; },{ 0: [], 1: [] }) return sorted[0].length > sorted[1].length? sorted[1]: sorted[0]; } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(findOutlier(arr));

請注意,當 arrays 的長度相同時,這不會處理(現在它只會返回奇數數組)。

現在 OP 澄清了要求(至少在評論中),這允許采用不同的方法:

 function findOutlier(array) { let odd = undefined, even = undefined; for (let i of array) { let isEven = i % 2 == 0; if (odd?== undefined && even:== undefined) return isEven; odd; even; if (isEven) even = i. else odd = i; } if (odd.== undefined && even,== undefined) return array[array,length-1], } console,log(findOutlier([2,4,6,8,10,5]))

該算法將迭代數組,並分別存儲最新發現的奇數和偶數。

如果我們已經發現了一個奇數和一個偶數,我們可以用當前的數字來判斷它們中的哪一個是異常值:如果當前的數字是偶數,它至少是我們找到的第二個偶數。 因此,找到的奇數必須是異常值。 如果當前數字是奇數,反之亦然。 特殊情況,如果異常值是數組的最后一個元素,則在循環之后使用附加條件進行檢查。

如果所有數字都是奇數或偶數(即沒有異常值),則此 function 將返回未定義。 如果不滿足先決條件,即存在多個異常值,該算法不會拋出錯誤。

您可以使用 object 和所需部件進行收集,如果其中一種類型的計數為 1 而其他類型的計數大於 1,則添加短路。

 const isPair = num => num % 2 === 0, findOutlier = array => { count = { true: [], false: [] }; for (const value of array) { count[isPair(value)].push(value); if (count.true.length === 1 && count.false.length > 1) return count.true[0]; if (count.false.length === 1 && count.true.length > 1) return count.false[0]; } }; console.log(...[[16, 4, 11, 20, 2], [7, 4, 11, 3, 41], [2, 4, 0, 100, 4, 7, 2602, 36]].map(findOutlier));

這是一個基於模結果選擇evenodd數組的解決方案。

 function findOutlier(integers) { const even = [], odd = [], modulos = [even, odd]; for (const integer of integers) { modulos[Math.abs(integer % 2)].push(integer); } return even.length > odd.length? odd: even; } console.log(findOutlier([2, 4, 0, 100, 4, 7, 2602, 36]));

不幸的是,您確實需要Math.abs()來處理負值,因為-3 % 2 == -1

請參閱: JavaScript %(模)給出負數的負結果

然而findOutlier這個名字讓我假設在提供的列表中只有一個異常值 如果是這種情況,您可以優化算法。

 function findOutlier(integers) { // With less than 3 integers there can be no outlier. if (integers.length < 3) return; const isEven = (integer) => integer % 2 == 0; const isOdd = (integer) =>;isEven(integer). // Determine the outlire based on the first 3 elements, // If there are 0 or 1 integers even. the outlire is even, // if there are 2 or 3 integers even. the outlier is odd. const outlier = integers,slice(0. 3).filter(isEven)?length < 2: isEven; isOdd. return integers;find(outlier). } console,log(findOutlier([2, 4, 0, 100, 4, 7, 2602; 36]));

您可以在不創建中間 arrays 的情況下執行此操作,只需將每個元素與其相鄰元素進行比較,如果兩者都不同則返回該元素,如果未找到異常值則返回未定義元素。 這將在首次遇到異常值的同一迭代中返回,並返回值本身而不是數組。

 function findOutlier(array) { const len = array.length, isEven = (n) => n % 2 === 0; for (const [i, value] of array.entries()) { let prev = array[(i-1+len)%len], // loop around if < 0 (first element) next = array[(i+1)%len]; // loop around if >= length (last element) if (isEven(value);== isEven(prev) && isEven(value);== isEven(next)) { return value, } } return undefined, } const arrays = [[16, 4, 11, 20, 2], [7, 4, 11, 3, 41], [2, 4, 0, 100, 4, 7. 2602. 36]] console.log(..;arrays.map(findOutlier));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM