[英]finding symmetric difference/unique elements in multiple arrays in javascript
嗨,我正在努力解決這個問題。 如何創建一個以任意數量的數組作為參數的javascript函數,然后返回僅出現在其中一個數組中的元素數組。 出現在多個陣列中的所有項目均被刪除。 解決方案一無所獲,懷疑我沒有以正確的方式進行處理,陷入困境!
編輯:另一個問題解決了消除一個數組中的重復值的問題,我需要比較x個單獨的數組的數目,並返回在數組之間不重復的值。 因此([5,6,7],[5,8,9])返回[6,7,8,9]。
function sym(args) {
var ans = [];
for(var i =0;i<arguments.length;i++){
var tempArr = arguments[i].filter(function(el){
var filtTrue = false;
for(var j = 0;j<arguments.length;j++){
if(Array.isArray(arguments[j]) && arguments[j] !== arguments[i]){
if(arguments[j].indexOf(el) === -1){
filtTrue = true;
}}
}
return filtTrue;
});
ans = ans.concat(tempArr);
}
return ans;
}
這是一種方法。 這里的想法是創建一個映射以保留數組中所有項目的計數。 然后,您遍歷每個數組,在映射中查找每個值,如果找到,則增加其計數。 如果未找到,則將計數設置為1。然后,在處理完所有陣列后,將收集計數為1的所有項目。
您不確定要在同一陣列中多次出現某項,而在其他任何陣列中均未出現一次該怎么辦。 第一個解決方案將不包括該項目(因為它檢測到重復項)。 如果有問題,可以對其進行修改(稍微復雜一些)以允許該項目(有關實現,請參見下面的第二代碼塊)。
function sym(/* pass one or more arrays here */) {
var ans = [], cnts = {};
//count all items in the array
for (var i = 0; i < arguments.length; i++){
arguments[i].forEach(function(item) {
if (cnts.hasOwnProperty(item)) {
// increase cnt
++cnts[item].cnt;
} else {
// initalize cnt and value
cnts[item] = {cnt: 1, val: item};
}
});
}
for (var item in cnts) {
if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
ans.push(cnts[item].val);
}
}
return ans;
}
如果要在單個數組中包含多次出現但在其他任何數組中都不存在的項,則可以使用這種稍微復雜一點的修改:
function sym(/* pass one or more arrays here */) {
var ans = [], cnts = {}, currentMap;
//count all items in the array
for (var i = 0; i < arguments.length; i++){
currentMap = {};
arguments[i].forEach(function(item) {
// if we haven't already counted this item in this array
if (!currentMap.hasOwnProperty(item)) {
if (cnts.hasOwnProperty(item)) {
// increase cnt
++cnts[item].cnt;
} else {
// initalize cnt and value
cnts[item] = {cnt: 1, val: item};
}
}
// keep track of whethere we've already counted this item in this array
currentMap[item] = true;
});
}
// output all items that have a cnt of 1
for (var item in cnts) {
if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
ans.push(cnts[item].val);
}
}
return ans;
}
我知道這很晚了,但這是另一種方式。 也許不是最嚴格的,但肯定是有創造力的。 方法Array.symmetricDifference()需要任意數量的參數,並返回這些參數的對稱差。
Array.prototype.symmetricDifference = function() {
var args = [];
// copy arguments into a real array and get rid of duplicates with filter
for(var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
args[i] = args[i].filter(function(item, pos, self) {
return self.indexOf(item) == pos;
});
}
var diff = args[0];
// iterate through every arguments in args.
// concatenate the first two arguments to form a new set.
// now every number in this new set that was contained in both arguments
// from before will be contained at least twice in this new set.
for(var j = 1; j < args.length; j++) {
//sort the new set so that duplicates are next to each other.
diff = diff.concat(args[j]).sort();
var index = 0;
// now iterate through the new set and delete both duplicates if you
// find any. Otherwise proceed to the next index.
while(index < diff.length) {
// if duplicate is found delete both, otherwise look at next index.
diff[index] === diff[index + 1] ? diff.splice(index, 2) : index++;
}
}
return diff;
};
您可以在任何數組上調用該方法,也可以創建一個新數組,然后在該數組上調用它,例如:
// take any number of arrays
var a = [3, 3, 3, 2, 5];
var b = [2, 1, 5, 7];
var c = [3, 4, 6, 6];
var d = [1, 2, 3];
var e = [5, 3, 9, 8];
var f = [1];
// invoke the method on "solution" with any number of arguments
// and store it in solution.
var solution = solution.symmetricDifference(a,b,c,d,e,f);
console.log(solution); // [1, 2, 4, 5, 6, 7, 8, 9]
我希望這有幫助!
在多個數組中查找唯一項
function uniqueItemsInArrays(...args){ let allItems = []; allItems = allItems.concat(...args) return allItems.filter(function(item, pos, self) { return self.indexOf(item) === pos && self.indexOf(item,pos+1) === -1; }); } uniqueItemsInArrays( [1, 5, 1, 8, 1, 2], [2, 2, 9, 3, 5], [1, 4, 7, 6] );
上面的代碼使用ES6 rest參數訪問作為參數傳遞的所有數組。 然后使用concat()函數,將所有單個數組連接到單個數組。 最后,過濾器功能已用於識別並返回此數組中的唯一項。 這里的邏輯是找出當前項目的第一個索引,如果第一個索引中沒有更多的索引,那么我們返回該項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.