如何统计javascript中数组的重复值
目前,我有这样一个数组:
var uniqueCount = Array();
几步后,我的数组看起来像这样:
uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
我该如何计算arrays中有多less个a,b,c? 我想得到如下结果:
a = 3 b = 1 c = 2 d = 2
等等
function count() { array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"]; array_elements.sort(); var current = null; var cnt = 0; for (var i = 0; i < array_elements.length; i++) { if (array_elements[i] != current) { if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times<br>'); } current = array_elements[i]; cnt = 1; } else { cnt++; } } if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times'); } }
演示小提琴
var counts = {}; your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
像这样的东西:
uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"]; var count = {}; uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;}); console.log(count);
我偶然发现了这个(很古老)的问题。 有趣的是,最明显和优雅的解决scheme(imho)缺失: Array.prototype.reduce(…) 。 自2011年左右(IE)以来,所有主stream浏览器都支持此function,甚至更早(所有其他):
var arr = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a']; var map = arr.reduce(function(prev, cur) { prev[cur] = (prev[cur] || 0) + 1; return prev; }, {}); // map is an associative array mapping the elements to their frequency: document.write(JSON.stringify(map)); // prints {"a": 3, "b": 2, "c": 2, "d": 2, "e": 2, "f": 1, "g": 1, "h": 3}
你可以这样做:
uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a']; var map = new Object(); for(var i = 0; i < uniqueCount.length; i++) { if(map[uniqueCount[i]] != null) { map[uniqueCount[i]] += 1; } else { map[uniqueCount[i]] = 1; } }
现在你有一个包含所有字符的地图
你可以解决它,而不用for / while循环。
function myCounter(inputWords) { return inputWords.reduce( (countWords, word) => { countWords[word] = ++countWords[word] || 1; return countWords; }, {}); }
希望它可以帮助你!
我认为这是最简单的方法如何计数相同的数值发生。
var a = [true, false, false, false]; a.filter(function(value){ return value === false; }).length
你可以有一个包含计数的对象。 遍历列表并增加每个元素的计数:
var counts = {}; uniqueCount.forEach(function(element) { counts[element] = (counts[element] || 0) + 1; }); for (var element in counts) { console.log(element + ' = ' + counts[element]); }
var uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a']; // here we will collect only unique items from the array var uniqueChars = []; // iterate through each item of uniqueCount for (i of uniqueCount) { // if this is an item that was not earlier in uniqueCount, // put it into the uniqueChars array if (uniqueChars.indexOf(i) == -1) { uniqueChars.push(i); } } // after iterating through all uniqueCount take each item in uniqueChars // and compare it with each item in uniqueCount. If this uniqueChars item // corresponds to an item in uniqueCount, increase letterAccumulator by one. for (x of uniqueChars) { let letterAccumulator = 0; for (i of uniqueCount) { if (i == x) {letterAccumulator++;} } console.log(`${x} = ${letterAccumulator}`); }
好的答案组合:
var count = {}; var arr = ['a', 'b', 'c', 'd', 'd', 'e', 'a', 'b', 'c', 'f', 'g', 'h', 'h', 'h', 'e', 'a']; var iterator = function (element) { count[element] = (count[element] || 0) + 1; } if (arr.forEach) { arr.forEach(function (element) { iterator(element); }); } else { for (var i = 0; i < arr.length; i++) { iterator(arr[i]); } }
希望这是有帮助的。
这一个我在github上find更多的控制重复计数
public class CalculateCount { public static void main(String[] args) { int a[] = {1,2,1,1,5,4,3,2,2,1,4,4,5,3,4,5,4}; Arrays.sort(a); int count=1; int i; for(i=0;i<a.length-1;i++){ if(a[i]!=a[i+1]){ System.out.println("The Number "+a[i]+" appears "+count+" times"); count=1; } else{ count++; } } System.out.println("The Number "+a[i]+" appears "+count+" times"); }
}
在包含字母的数组中重复:
var arr = ["a", "b", "a", "z", "e", "a", "b", "f", "d", "f"], sortedArr = [], count = 1; sortedArr = arr.sort(); for (var i = 0; i < sortedArr.length; i = i + count) { count = 1; for (var j = i + 1; j < sortedArr.length; j++) { if (sortedArr[i] === sortedArr[j]) count++; } document.write(sortedArr[i] + " = " + count + "<br>"); }
通过使用array.map我们可以减less循环,请参阅jsfiddle
function Check(){ var arr = Array.prototype.slice.call(arguments); var result = []; for(i=0; i< arr.length; i++){ var duplicate = 0; var val = arr[i]; arr.map(function(x){ if(val === x) duplicate++; }) result.push(duplicate>= 2); } return result; }
去testing:
var test = new Check(1,2,1,4,1); console.log(test);
基于减less数组函数的单行
const uniqueCount = ["a", "b", "c", "d", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"]; const distribution = uniqueCount.reduce((acum,cur) => Object.assign(acum,{[cur]: (acum[cur] | 0)+1}),{}); console.log(JSON.stringify(distribution,null,2));