最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how do I remove duplicates in Javascript array with out sorting - Stack Overflow

programmeradmin0浏览0评论

So I am making a game that calculates love percentage based on the letters in two peoples names with the word love.

In the code below I am counting the number of times a letter appears in the value array then I want to push this value into the nums Array for each unique letter. However I am not sure how I can get it to not repeat for each time the letter appears in the value array. Also I can not sort the array or the game will not calculate properly.

so for example the below value array should push 3 for s only once into the nums array.

var value =["s", "a", "m", "a", "n", "t", "h", "a", "w", "h", "i", "t", "e", "l", "o", "v", "e", "s", "J", "a", "c", "k", "h", "a", "r", "r", "i", "s"]; 


function trueLoveGame(value){

    var nums=[];

    for(var i =0; i < value.length;i++){
        var count = 0;
        for(var a = 0; a < value.length;a++){
            if(value[i] === value[a]){  
                count++;
            }
            else{

            }
        }   
    }
}

So I am making a game that calculates love percentage based on the letters in two peoples names with the word love.

In the code below I am counting the number of times a letter appears in the value array then I want to push this value into the nums Array for each unique letter. However I am not sure how I can get it to not repeat for each time the letter appears in the value array. Also I can not sort the array or the game will not calculate properly.

so for example the below value array should push 3 for s only once into the nums array.

var value =["s", "a", "m", "a", "n", "t", "h", "a", "w", "h", "i", "t", "e", "l", "o", "v", "e", "s", "J", "a", "c", "k", "h", "a", "r", "r", "i", "s"]; 


function trueLoveGame(value){

    var nums=[];

    for(var i =0; i < value.length;i++){
        var count = 0;
        for(var a = 0; a < value.length;a++){
            if(value[i] === value[a]){  
                count++;
            }
            else{

            }
        }   
    }
}
Share Improve this question edited Apr 20, 2013 at 15:42 Susan Delgado asked Apr 20, 2013 at 15:36 Susan DelgadoSusan Delgado 1402 silver badges10 bronze badges 3
  • 2 see if the letter's in the already before pushing it, then. or you should be using the letters as the array keys instead, e.g. letters['s'] = 3, so you don't have to worry about uniques - using the letters as keys will automatically make each letter unique, because you can't have duplicate keys. – Marc B Commented Apr 20, 2013 at 15:39
  • Im not sure what that means? – Susan Delgado Commented Apr 20, 2013 at 15:43
  • what is the output you are expecting ? an object or array ? – Diode Commented Apr 20, 2013 at 15:43
Add a ment  | 

2 Answers 2

Reset to default 3

So as it wasn't clear exactly what was required. This should answer both possibilities that I think you could be asking.

var userArray = ["s", "a", "m", "a", "n", "t", "h", "a", "w", "h", "i", "t", "e", "l", "o", "v", "e", "s", "J", "a", "c", "k", "h", "a", "r", "r", "i", "s"];

function unique(array) {
    var unique = [];

    array.forEach(function (value) {
        if (unique.indexOf(value) === -1) {
            unique.push(value);
        }
    });

    return unique;
}

function count(array) {
    var obj = {};

    array.forEach(function (value) {
        obj[value] = (obj[value] || 0) + 1;
    });

    return obj;
}


console.log(userArray);
console.log(unique(userArray));
console.log(count(userArray));

on jsfiddle

I am not sure whether you need to get key value pairs. You can try something as given below to get an object of letter-count pairs.

function trueLoveGame(value){


    var nums={};

    for(var i =0; i < value.length;i++){
       var letter = value[i];
       nums[letter] =   nums[letter] || 0;
       nums[letter]++;
    }

    return nums;
}
发布评论

评论列表(0)

  1. 暂无评论