I'm trying to take an array (for an example, an array of years) and then make a new array of sub-arrays which tells, firstly, the unique element in the original array and, secondly, how many time it was repeated.
我正在尝试获取一个数组(例如,一个数组的数组),然后创建一个新的子数组数组,首先说明原始数组中的唯一元素,其次,它重复了多少次。
For example, lets say I start of with an array of numbers [1999, 1999, 2000, 2005, 2005, 2005, 2015]
例如,假设我从一系列数字开始[1999,1999,2000,2005,2005,2005,2015]
I would like my function to return an array like [[1999, 2], [2000, 1], [2005, 3], [2015, 1] ]
because the year 1999 was repeated twice, the year 2000 was was not repeated, the year 2005 was repeated three times, etc.
我想我的函数返回像[[1999,2],[2000,1],[2005,3],[2015,1]]这样的数组,因为1999年重复了两次,2000年没有重复,2005年重复了三次,等等。
I can successfully make a new array that removes the duplicates, but Im getting some weird behavior when it comes to making my sub-arrays.
我可以成功地创建一个删除重复项的新数组,但是在制作我的子数组时我得到了一些奇怪的行为。
EDIT
编辑
Here was my own faulty solution, in case anyone wants to point out what i did wrong.
这是我自己的错误解决方案,万一有人想指出我做错了什么。
var populateYearsList = function(yearsDuplicate){
var uniqueYears = [];
for (var i=0; i < yearsDuplicate.length; i++){
if(uniqueYears.indexOf(yearsDuplicate[i]) == -1){
uniqueYears.push(yearsDuplicate[i])
} else {
console.log("duplicate found") };
}
console.log (uniqueYears)
};
I ran into the problem of trying to change uniqueYears.push(yearsDuplicate[i])
into uniqueYears.push([ yearsDuplicate[i], 1 ])
and then trying to replace my console.log into some sort of incremented counter.
我遇到了尝试将uniqueYears.push(yearsDuplicate [i])更改为uniqueYears.push([yearsDuplicate [i],1])然后尝试将我的console.log替换为某种递增计数器的问题。
4 个解决方案
#1
4
It is as easy as:
它很简单:
var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
var uniq = [];
input.forEach(function(n){
if (uniq.indexOf(n)<0)
uniq.push(n);
});
var output = uniq.map(function(n){
return [n, input.filter(function(m){ return m == n }).length]
});
Quick explanation how it works
快速解释它是如何工作的
Given the input array, map its unique version to the new array with this mapping:
给定输入数组,使用此映射将其唯一版本映射到新数组:
element ---> [ element, the number of occurrences in the original array ]
element ---> [element,原始数组中出现的次数]
EDIT NOTE:: FIXED the previous solution which might introduced duplicate element.
编辑注意::修复了可能引入重复元素的先前解决方案。
#2
3
This is how I would do it:
我就是这样做的:
var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
function numberOfOccurancesArray (input) {
var result = {};
for (var i = 0; i < input.length; i++) {
var currentYear = input[i];
// if there is an element with the name of the currentYear
if (result[currentYear]) {
// increace its prop `count` with 1
result[currentYear].count += 1;
} else {
// if not present, create it
result[currentYear] = {
year: currentYear,
count: 1
}
}
}
return result;
}
Here is sample code : JsFiddle
以下是示例代码:JsFiddle
#3
1
If you want to play with functional paradigms, reduce it to a dictionary and then map the keys to a tuple.
如果您想要使用功能范例,请将其缩小为字典,然后将键映射到元组。
var yearToCounts = input.reduce(function(counts, year) {
if (year in counts) {
counts[year]++;
} else {
counts[year] = 1;
}
return counts;
}, {});
return Object.keys(yearToCounts).map(function(year) {
return [year, yearToCounts[year]];
});
#4
1
var years = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
var occurences = [];
for(var i = 0; i < years.length; i++ ) {
var year = years[i];
if(typeof occurences[year] === 'undefined') {
occurences[year] = 1;
} else {
occurences[year]++;
}
}
occurences = occurences.map(function(occurences, year) {
return [year, occurences];
});
console.log(occurences);
EDIT: Much faster solution
编辑:更快的解决方案
var results = [];
var uniq = [];
var counts = [];
var i = 0;
for (i; i < years.length; i++) {
var year = years[i];
var indexOf = uniq.indexOf(year);
if (indexOf < 0) {
uniq.push(year);
counts[uniq.length - 1] = 1;
} else {
counts[indexOf] += 1;
}
}
i = 0;
for (i; i < uniq.length; i++) {
results.push([uniq[i], counts[i]]);
}
return results;
#1
4
It is as easy as:
它很简单:
var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
var uniq = [];
input.forEach(function(n){
if (uniq.indexOf(n)<0)
uniq.push(n);
});
var output = uniq.map(function(n){
return [n, input.filter(function(m){ return m == n }).length]
});
Quick explanation how it works
快速解释它是如何工作的
Given the input array, map its unique version to the new array with this mapping:
给定输入数组,使用此映射将其唯一版本映射到新数组:
element ---> [ element, the number of occurrences in the original array ]
element ---> [element,原始数组中出现的次数]
EDIT NOTE:: FIXED the previous solution which might introduced duplicate element.
编辑注意::修复了可能引入重复元素的先前解决方案。
#2
3
This is how I would do it:
我就是这样做的:
var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
function numberOfOccurancesArray (input) {
var result = {};
for (var i = 0; i < input.length; i++) {
var currentYear = input[i];
// if there is an element with the name of the currentYear
if (result[currentYear]) {
// increace its prop `count` with 1
result[currentYear].count += 1;
} else {
// if not present, create it
result[currentYear] = {
year: currentYear,
count: 1
}
}
}
return result;
}
Here is sample code : JsFiddle
以下是示例代码:JsFiddle
#3
1
If you want to play with functional paradigms, reduce it to a dictionary and then map the keys to a tuple.
如果您想要使用功能范例,请将其缩小为字典,然后将键映射到元组。
var yearToCounts = input.reduce(function(counts, year) {
if (year in counts) {
counts[year]++;
} else {
counts[year] = 1;
}
return counts;
}, {});
return Object.keys(yearToCounts).map(function(year) {
return [year, yearToCounts[year]];
});
#4
1
var years = [1999, 1999, 2000, 2005, 2005, 2005, 2015];
var occurences = [];
for(var i = 0; i < years.length; i++ ) {
var year = years[i];
if(typeof occurences[year] === 'undefined') {
occurences[year] = 1;
} else {
occurences[year]++;
}
}
occurences = occurences.map(function(occurences, year) {
return [year, occurences];
});
console.log(occurences);
EDIT: Much faster solution
编辑:更快的解决方案
var results = [];
var uniq = [];
var counts = [];
var i = 0;
for (i; i < years.length; i++) {
var year = years[i];
var indexOf = uniq.indexOf(year);
if (indexOf < 0) {
uniq.push(year);
counts[uniq.length - 1] = 1;
} else {
counts[indexOf] += 1;
}
}
i = 0;
for (i; i < uniq.length; i++) {
results.push([uniq[i], counts[i]]);
}
return results;