首先,很抱歉,这是一个简单的问题,但是我很难在程序中选择唯一的数组(我只是在学习javasript btw)。我知道我可以使用内置功能(例如过滤器或地图)轻松完成此操作,但是我被禁止这样做。 这是我到目前为止所得到的:
function countUnique (numbers) {
var sorted_numbers = numbers.sort(function(a,b){return a-b})
var result = []
for (var i = 0 ; i<numbers.length; i++){
if (numbers[i]!==numbers[i+1]){
result.push(numbers[i])
console.log(result)
}
}
var sum = 0
for (var j = 0; j<result.length; j++) {
sum = sum + result[j]
}
return sum
}
console.log(countUnique([ 5, 5, 6, 6, 3, 1, 2, 7, 7])) // 6
console.log(countUnique([ 3, 6, 3, 6, 1, 1, 2, 1 ])) // 2
console.log(countUnique([ 3, 3, 3, 3, 4, 5, 8, 10, 11 ])) // 38
示例: 输入:[5,5,6,6,3,1,2,7,7] 散文:3 +1 + 2 结果:6
答案 0 :(得分:5)
使用indexOf和lastIndexOf。如果indexOf的结果等于lastIndexOf的结果,则意味着该元素在数组中是唯一的。
let arr = [ 3, 3, 3, 3, 4, 5, 8, 10, 11 ];
let sum = 0;
arr.forEach(a => {
sum += (arr.indexOf(a) === arr.lastIndexOf(a)) ? a : 0;
});
console.log(sum);
答案 1 :(得分:2)
设置这样的内部循环
for (var i = 0 ; i<numbers.length; i++){
for(var j = 0; j<numbers.length; j++) {
if (numbers[i] + 1 === numbers[j]){
result.push(numbers[i])
console.log(result)
}
}
}
或自排序以来用于执行更少的循环
function countUnique(numbers) {
var sorted_numbers = numbers.sort(function(a,b){return a-b})
var result = []
/*
for (var i = 0 ; i < numbers.length; i++){
for(var j = 0; j < sorted_numbers.length; j++) {
if (numbers[i] + 1 === sorted_numbers[j]){
result.push(numbers[i]);
}
}
}
*/
var working = false;
var current_count = 0;
var counted_values = [];
//create a new array for counting which numbers were used.
for(i = 0; i < sorted_numbers.length; i++) {
counted_values.push(0);
}
var current_value = 0;
var put = false;
for(var i = 0; i < sorted_numbers.length; i++) {
current_value = sorted_numbers[i];
put = false;
for(var j = 0; j < sorted_numbers.length; j++) {
if (current_value + 1 === sorted_numbers[j] && counted_values[j] == 0 && counted_values[i] == 0) {
if(!put) {
result.push(current_value);
put = true;
}
result.push(sorted_numbers[j]);
counted_values[j] = 1;
current_value = sorted_numbers[j];
}
}
}
console.log(result);
var sum = 0;
for (var j = 0; j < result.length; j++) {
sum = sum + result[j];
}
return sum;
}
console.log(countUnique([ 5, 5, 6, 6, 3, 1, 2, 7, 7])); // 6
console.log(countUnique([ 3, 6, 3, 6, 1, 1, 2, 1 ])); // 2
console.log(countUnique([ 3, 3, 3, 3, 4, 5, 8, 10, 11 ])); // 38
答案 2 :(得分:2)
您可以使用对象来确定唯一编号。这可能有点复杂,但是如果您的数组很长,它可能会非常有用。
function countUnique(numbers){
var counter={};
var sum=0;
//Record all numbers accordingly
for(var n in numbers){
//The current number in the array
var num=numbers[n];
/*
This basically checks if the counter object contains the property
(Which is any number in the array). If it doesn't contain it then it
will add it as a property with a property value of 1
(meaning there is currently one unique number in the array). If it does
contain it then it will simply add 1 to the property value indicating there is
more than one of the same number
*/
(counter[num]==null)?counter[num]=1:counter[num]++;
}
/*
After we are done with going through the array, we will go
through the object properties and only add the ones with a value of 1
(indicating they are unique) and add 0 if the property's value is more
than one
*/
for(var c in counter){
sum+=(counter[c]==1)?c:0;
}
return sum;
}
console.log(countUnique([ 5, 5, 6, 6, 3, 1, 2, 7, 7])); // 6
console.log(countUnique([ 3, 6, 3, 6, 1, 1, 2, 1 ])); // 2
console.log(countUnique([ 3, 3, 3, 3, 4, 5, 8, 10, 11 ])); // 38
答案 3 :(得分:1)
// Here you have the exact same arrays you used in your question
const numbers1 = [ 5, 5, 6, 6, 3, 1, 2, 7, 7]; // 6
const numbers2 = [ 3, 6, 3, 6, 1, 1, 2, 1 ]; // 2
const numbers3 = [ 3, 3, 3, 3, 4, 5, 8, 10, 11 ]; // 38
// Here we have a function that finds non-repeating numbers
// We don't only remove duplicates if a number appears more than once, we don't include it at all
function notRepeatedNum(arr) {
let result = [];
let ctr = 0;
for (let x = 0; x < arr.length; x++) {
ctr = 0;
for (let y = 0; y < arr.length; y++) {
if (arr[x] === arr[y]) { // If we have a match increment the counter by 1
ctr+= 1;
}
}
// If the counter is >= 2 then the number is repeating, otherwise push it to the array
if (ctr < 2) {
result.push(arr[x]);
}
}
return result;
}
// Here we use the function to find those non-repeating and store it in a variable
const nonReapiting1 = notRepeatedNum(numbers1);
const nonReapiting2 = notRepeatedNum(numbers2);
const nonReapiting3 = notRepeatedNum(numbers3);
// Here we are summing the non-repeating numbers into a single value
function sumIt(numbers) {
let sum = 0;
for (let b = 0; b < numbers.length; b++) {
sum += numbers[b];
}
return sum;
}
// And finally we log it to the console :)
console.log(sumIt(nonReapiting1)); // 6
console.log(sumIt(nonReapiting2)); // 2
console.log(sumIt(nonReapiting3)); // 38
注意: 使用数组帮助程序可以更快,更清洁,更有效地完成此任务,但是我很确定这就是您所要求的。没有Babel / ES6,甚至没有使用过的对象软件:)
运行代码段,看看是否适合您