我有一个重复值的数组。我想找到任何给定值的出现次数。
例如,如果我有一个如下定义的数组:var dataset = [2,2,4,2,6,4,7,8];
,我想找到数组中某个值的出现次数。也就是说,程序应该显示如果我有3次出现值2
,则出现值6
,等等。
这是最惯用/优雅的方法吗?
答案 0 :(得分:81)
reduce
在这里比filter
更合适,因为它不会为了计数而构建临时数组。
var dataset = [2,2,4,2,6,4,7,8];
var search = 2;
var count = dataset.reduce(function(n, val) {
return n + (val === search);
}, 0);
console.log(count);
在ES6中:
let count = dataset.reduce((n, x) => n + (x === search), 0);
请注意,扩展它以使用自定义匹配谓词很容易,例如,计算具有特定属性的对象:
people = [
{name: 'Mary', gender: 'girl'},
{name: 'Paul', gender: 'boy'},
{name: 'John', gender: 'boy'},
{name: 'Lisa', gender: 'girl'},
{name: 'Bill', gender: 'boy'},
{name: 'Maklatura', gender: 'girl'}
]
var numBoys = people.reduce(function (n, person) {
return n + (person.gender == 'boy');
}, 0);
console.log(numBoys);
计算所有项目,即使{x:count of xs}
之类的对象在javascript中很复杂,因为对象键只能是字符串,因此您无法可靠地计算具有混合类型的数组。不过,以下简单的解决方案在大多数情况下都能正常运行:
count = function (ary, classifier) {
classifier = classifier || String;
return ary.reduce(function (counter, item) {
var p = classifier(item);
counter[p] = counter.hasOwnProperty(p) ? counter[p] + 1 : 1;
return counter;
}, {})
};
people = [
{name: 'Mary', gender: 'girl'},
{name: 'Paul', gender: 'boy'},
{name: 'John', gender: 'boy'},
{name: 'Lisa', gender: 'girl'},
{name: 'Bill', gender: 'boy'},
{name: 'Maklatura', gender: 'girl'}
];
// If you don't provide a `classifier` this simply counts different elements:
cc = count([1, 2, 2, 2, 3, 1]);
console.log(cc);
// With a `classifier` you can group elements by specific property:
countByGender = count(people, function (item) {
return item.gender
});
console.log(countByGender);
在ES6中,您使用Map
对象可靠地计算任意类型的对象。
class Counter extends Map {
constructor(iter, key=null) {
super();
this.key = key || (x => x);
for (let x of iter) {
this.add(x);
}
}
add(x) {
x = this.key(x);
this.set(x, (this.get(x) || 0) + 1);
}
}
// again, with no classifier just count distinct elements
results = new Counter([1, 2, 3, 1, 2, 3, 1, 2, 2]);
for (let [number, times] of results.entries())
console.log('%s occurs %s times', number, times);
// counting objects
people = [
{name: 'Mary', gender: 'girl'},
{name: 'John', gender: 'boy'},
{name: 'Lisa', gender: 'girl'},
{name: 'Bill', gender: 'boy'},
{name: 'Maklatura', gender: 'girl'}
];
chessChampions = {
2010: people[0],
2012: people[0],
2013: people[2],
2014: people[0],
2015: people[2],
};
results = new Counter(Object.values(chessChampions));
for (let [person, times] of results.entries())
console.log('%s won %s times', person.name, times);
// you can also provide a classifier as in the above
byGender = new Counter(people, x => x.gender);
for (let g of ['boy', 'girl'])
console.log("there are %s %ss", byGender.get(g), g);
Counter
的类型感知实现可能如下所示(Typescript):
type CounterKey = string | boolean | number;
interface CounterKeyFunc<T> {
(item: T): CounterKey;
}
class Counter<T> extends Map<CounterKey, number> {
key: CounterKeyFunc<T>;
constructor(items: Iterable<T>, key: CounterKeyFunc<T>) {
super();
this.key = key;
for (let it of items) {
this.add(it);
}
}
add(it: T) {
let k = this.key(it);
this.set(k, (this.get(k) || 0) + 1);
}
}
// example:
interface Person {
name: string;
gender: string;
}
let people: Person[] = [
{name: 'Mary', gender: 'girl'},
{name: 'John', gender: 'boy'},
{name: 'Lisa', gender: 'girl'},
{name: 'Bill', gender: 'boy'},
{name: 'Maklatura', gender: 'girl'}
];
let byGender = new Counter<Person>(people, (p: Person) => p.gender);
for (let g of ['boy', 'girl'])
console.log("there are %s %ss", byGender.get(g), g);
答案 1 :(得分:11)
较新的浏览器仅限于使用Array.filter
var dataset = [2,2,4,2,6,4,7,8];
var search = 2;
var occurrences = dataset.filter(function(val) {
return val === search;
}).length;
console.log(occurrences); // 3
答案 2 :(得分:10)
array.filter(c => c === searchvalue).length;
答案 3 :(得分:8)
以下是一种显示所有计数的方法:
var dataset = [2, 2, 4, 2, 6, 4, 7, 8];
var counts = {}, i, value;
for (i = 0; i < dataset.length; i++) {
value = dataset[i];
if (typeof counts[value] === "undefined") {
counts[value] = 1;
} else {
counts[value]++;
}
}
console.log(counts);
// Object {
// 2: 3,
// 4: 2,
// 6: 1,
// 7: 1,
// 8: 1
//}
答案 4 :(得分:6)
使用正常循环,您可以一致且可靠地找到事件:
const dataset = [2,2,4,2,6,4,7,8];
function getNumMatches(array, valToFind) {
let numMatches = 0;
for (let i = 0, j = array.length; i < j; i += 1) {
if (array[i] === valToFind) {
numMatches += 1;
}
}
return numMatches;
}
alert(getNumMatches(dataset, 2)); // should alert 3
DEMO: https://jsfiddle.net/a7q9k4uu/
为了使它更通用,该函数可以接受具有自定义逻辑(返回true
/ false
)的谓词函数,该函数将确定最终计数。例如:
const dataset = [2,2,4,2,6,4,7,8];
function getNumMatches(array, predicate) {
let numMatches = 0;
for (let i = 0, j = array.length; i < j; i += 1) {
const current = array[i];
if (predicate(current) === true) {
numMatches += 1;
}
}
return numMatches;
}
const numFound = getNumMatches(dataset, (item) => {
return item === 2;
});
alert(numFound); // should alert 3
答案 5 :(得分:5)
var dataset = [2,2,4,2,6,4,7,8], count = {}
dataset.forEach(function(el){
count[el] = count[el] + 1 || 1
});
console.log(count)
// {
// 2: 3,
// 4: 2,
// 6: 1,
// 7: 1,
// 8: 1
// }
答案 6 :(得分:3)
您可以在array.reduce(callback[, initialValue])
JavaScript 1.8
方法
var dataset = [2,2,4,2,6,4,7,8],
dataWithCount = dataset.reduce( function( o , v ) {
if ( ! o[ v ] ) {
o[ v ] = 1 ;
} else {
o[ v ] = o[ v ] + 1;
}
return o ;
}, {} );
// print data with count.
for( var i in dataWithCount ){
console.log( i + 'occured ' + dataWithCount[i] + 'times ' );
}
// find one number
var search = 2,
count = dataWithCount[ search ] || 0;
答案 7 :(得分:3)
您可以使用reduce计算数组中的所有项目。
[].reduce((a,b) => (a[b] = a[b] + 1 || 1) && a, {})
这将产生一个对象,其键是数组中的不同元素,值是数组中元素出现的计数。然后,您可以通过访问对象上的相应密钥来访问一个或多个计数。
例如,如果您将上述内容包含在名为count()
的函数中:
function count(arr) {
return arr.reduce((a,b) => (a[b] = a[b] + 1 || 1) && a, {})
}
count(['example']) // { example: 1 }
count([2,2,4,2,6,4,7,8])[2] // 3
答案 8 :(得分:0)
我发现最终得到一个对象列表更有用,它包含一个用于计算内容的键和一个计数键:
const data = [2,2,4,2,6,4,7,8]
let counted = []
for (var c of data) {
const alreadyCounted = counted.map(c => c.name)
if (alreadyCounted.includes(c)) {
counted[alreadyCounted.indexOf(c)].count += 1
} else {
counted.push({ 'name': c, 'count': 1})
}
}
console.log(counted)
返回:
[ { name: 2, count: 3 },
{ name: 4, count: 2 },
{ name: 6, count: 1 },
{ name: 7, count: 1 },
{ name: 8, count: 1 } ]
这不是最干净的方法,如果有人知道如何用reduce
获得相同的结果,请告诉我。但是,它确实产生了一个相当容易使用的结果。
答案 9 :(得分:0)
首先,您可以使用线性搜索来使用Brute Force Solution。
public int LinearSearchcount(int[] A, int data){
int count=0;
for(int i=0;i<A.length;i++) {
if(A[i]==data) count++;
}
return count;
}
然而,为此,我们将时间复杂度定为O(n)。但是通过二进制搜索,我们可以提高复杂性。