How to get unique values in an array

后端 未结 20 2388
情歌与酒
情歌与酒 2020-11-22 14:02

How can I get a list of unique values in an array? Do I always have to use a second array or is there something similar to java\'s hashmap in JavaScript?

I am going

20条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 14:38

    I have tried this problem in pure JS. I have followed following steps 1. Sort the given array, 2. loop through the sorted array, 3. Verify previous value and next value with current value

    // JS
    var inpArr = [1, 5, 5, 4, 3, 3, 2, 2, 2,2, 100, 100, -1];
    
    //sort the given array
    inpArr.sort(function(a, b){
        return a-b;
    });
    
    var finalArr = [];
    //loop through the inpArr
    for(var i=0; i

    Demo

提交回复
热议问题