對不起,我錯誤地描述了我的問題:
如果在這樣的呼叫中引數值為“macOS” -> countDuplicate(operatingSystem, "macOS");
該函式必須回傳 9 的數字。其他值(Windows、Unix...)也是如此。
謝謝 !
let operatingSystem = [
["macOS", "Windows", "Unix"],
["macOS", ["Windows", "Unix", "macOS"], "Unix"],
[["macOS", "Windows", "Unix"], "Windows", "Unix"],
["Unix", "macOS", ["Windows", "Unix", "macOS"]],
[["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
[["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
];
function countDuplicate(array, arg) {
let count = 0;
for (let i = 0; i < operatingSystem.length; i ) {
for (let j = 0; j < operatingSystem[i].length; j ) {
for (let k = 0; k < operatingSystem[i][j].length; k ) {
for (let l = 0; l < operatingSystem[i][j][k].length; l ) {
let str = operatingSystem[i][j][k][l];
if (str.indexOf(arg) > -1) {
count = 1;
break;
}
}
}
}
}
console.log("There is " count " of " arg " similar items in this array.");
}
countDuplicate(operatingSystem, "macOS");
uj5u.com熱心網友回復:
首先你flat()多維陣列,然后使用這個解決方案:https : //stackoverflow.com/a/19395302/8205497
let operatingSystem = [
["macOS", "Windows", "Unix"],
["macOS", ["Windows", "Unix", "macOS"], "Unix"],
[["macOS", "Windows", "Unix"], "Windows", "Unix"],
["Unix", "macOS", ["Windows", "Unix", "macOS"]],
[["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
[["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"]
];
let counts = {};
operatingSystem.flat(4).forEach(function (x) { counts[x] = (counts[x] || 0) 1; });
console.log(counts);
let sum = 0;
Object.values(counts).forEach(x => sum = x)
console.log(`The total sum is: ${sum}`)
uj5u.com熱心網友回復:
您可以使用遞回來計算所有元素。你也可以做一個平面陣列。
在遞回中,如果元素是陣列迭代并呼叫其他每個值添加到映射。
let operatingSystem = [
["macOS", "Windows", "Unix"],
["macOS", ["Windows", "Unix", "macOS"], "Unix"],
[["macOS", "Windows", "Unix"], "Windows", "Unix"],
["Unix", "macOS", ["Windows", "Unix", "macOS"]],
[["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
[["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
];
function countDuplicate(array, res = {}) {
if (typeof array === "object") {
array.forEach((x) => countDuplicate(x, res));
} else {
if (!res[array]) res[array] = 0;
res[array] ;
}
}
let res = {};
countDuplicate(operatingSystem, res);
console.log(res);
console.log(Object.entries(res));
uj5u.com熱心網友回復:
您可以使用遞回和 aSet來檢查陣列中的每個專案并查看它是否唯一。如果它是一個陣列,則使用遞回,否則,如果它不是唯一的,則添加到計數中。
function countDuplicate(array, arg) {
let count = 0;
const uniqueItems = new Set()
const checkArray = arr => {
for (let item of arr) {
if (Array.isArray(item)) {
checkArray(item)
} else if (uniqueItems.has(item)) {
count
} else {
uniqueItems.add(item)
}
}
}
checkArray(array)
console.log("There is " count " similar items in this array.");
}
let operatingSystem = [
["macOS", "Windows", "Unix"],
["macOS", ["Windows", "Unix", "macOS"], "Unix"],
[["macOS", "Windows", "Unix"], "Windows", "Unix"],
["Unix", "macOS", ["Windows", "Unix", "macOS"]],
[["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
[["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"]
];
countDuplicate(operatingSystem)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321821.html
標籤:javascript 算法 网络 逻辑
上一篇:Symfony5-現在作業的正則運算式驗證器不起作用
下一篇:在自定義鉤子中設定多個狀態
