简体   繁体   English

比较两个单字符数组并返回差值?

[英]Compare two arrays of single characters and return the difference?

I need to be able to compare, and get the difference, between two arrays containing single character letters appearing multiple times in each array. 我需要能够比较并获得两个包含单个字符字母的数组在每个数组中出现多次的区别。

Example: 例:

arr_a = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "_", "S", "E", "L", "U", "V"]

arr_b = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "G", "S", "E", "L", "U", "V", "O", "I", "A", "R", "E", "S", "E"]

I've tried quite a few found on google, but none return the expected result. 我已经尝试了很多在google上找到的方法,但是没有一个方法返回预期的结果。 The problem seems to be with algorithms that loop through each letter, and then manipulate the result. 问题似乎出在循环遍历每个字母然后处理结果的算法上。 I need something that take into account the case that each letter can appear multiple times, and calculate the difference of how many times that actually happens. 我需要考虑到每个字母可以出现多次的情况,并计算实际发生多少次的差异。

Any hints? 有什么提示吗?

You could check each character at the same position and return only the parts who are different. 您可以在同一位置检查每个字符,然后仅返回不同的部分。

 function getDifference(first, second) { var min = Math.min(first.length, second.length), i = 0, result = []; while (i < min) { if (first[i] !== second[i]) { result.push(first[i], second[i]); } ++i; } return result.concat(first.slice(min), second.slice(min)); } var arr_a = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "_", "S", "E", "L", "U", "V"], arr_b = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "G", "S", "E", "L", "U", "V", "O", "I", "A", "R", "E", "S", "E"]; console.log(getDifference(arr_a, arr_b)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I have implemented a code just like you needed. 我已经按照您的需要实施了代码。

 arr_a = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "_", "S", "E", "L", "U", "V"] arr_b = ["E", "F", "X", "O", "U", "R", "T", "I", "D", "E", "N", "G", "Ø", "M", "F", "Æ", "A", "M", "Ø", "R", "Å", "N", "D", "E", "D", "R", "A", "G", "S", "E", "L", "U", "V", "O", "I", "A", "R", "E", "S", "E"] a_json = {}; b_json = {}; arr_a.forEach(function (element){ if(a_json.hasOwnProperty(element)){ a_json[element]++; } else{ a_json[element] = 1; } }); arr_b.forEach(function (element){ if(b_json.hasOwnProperty(element)){ b_json[element]++; } else{ b_json[element] = 1; } }); diff_json = {}; keys_a = Object.keys(a_json); keys_a.forEach(function(key_a){ if(b_json.hasOwnProperty(key_a)){ diff_json[key_a] = Math.abs(a_json[key_a] - b_json[key_a] ); } }); console.log(diff_json) 

What about transforming them into one (char -> number) object? 将它们转换为一个(字符->数字)对象怎么样?

 var keys={};
 arr1.forEach(function(e){
   keys[e]=keys[e]||0;
   keys[e]++;
 }
 arr2.forEach(function(e){
   keys[e]=keys[e]||0;
   keys[e]--;
 }
console.log(keys);

Keys will contain the difference between the appearance of a key in the two arrays. 键将包含两个数组中键的外观之间的差异。 Something like: 就像是:

"A":0, // A appears equal in both
"B":1, //B appears one more time in arr1 then in arr2
"C":-2 //C appears 2 times more in arr2 then in arr1

Or if you want to find the differences in arr2 compared to arr1 as an array of chars, do: 或者,如果您想通过字符数组找到与arr1相比在arr2中的差异,请执行以下操作:

var diff=arr2.map((e,i)=>e==arr1[i]?undefined:e);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM