How to get unique values in an array

后端 未结 20 1969
情歌与酒
情歌与酒 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:47

    Short and sweet solution using second array;

    var axes2=[1,4,5,2,3,1,2,3,4,5,1,3,4];
    
        var distinct_axes2=[];
    
        for(var i=0;i<axes2.length;i++)
            {
            var str=axes2[i];
            if(distinct_axes2.indexOf(str)==-1)
                {
                distinct_axes2.push(str);
                }
            }
        console.log("distinct_axes2 : "+distinct_axes2); // distinct_axes2 : 1,4,5,2,3
    
    0 讨论(0)
  • 2020-11-22 14:50

    Using EcmaScript 2016 you can simply do it like this.

     var arr = ["a", "a", "b"];
     var uniqueArray = Array.from(new Set(arr)); // Unique Array ['a', 'b'];
    

    Sets are always unique, and using Array.from() you can convert a Set to an array. For reference have a look at the documentations.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

    0 讨论(0)
  • 2020-11-22 14:51

    Majority of the solutions above have a high run time complexity.

    Here is the solution that uses reduce and can do the job in O(n) time.

    Array.prototype.unique = Array.prototype.unique || function() {
            var arr = [];
    	this.reduce(function (hash, num) {
    		if(typeof hash[num] === 'undefined') {
    			hash[num] = 1; 
    			arr.push(num);
    		}
    		return hash;
    	}, {});
    	return arr;
    }
        
    var myArr = [3,1,2,3,3,3];
    console.log(myArr.unique()); //[3,1,2];

    Note:

    This solution is not dependent on reduce. The idea is to create an object map and push unique ones into the array.

    0 讨论(0)
  • You can enter array with duplicates and below method will return array with unique elements.

    function getUniqueArray(array){
        var uniqueArray = [];
        if (array.length > 0) {
           uniqueArray[0] = array[0];
        }
        for(var i = 0; i < array.length; i++){
            var isExist = false;
            for(var j = 0; j < uniqueArray.length; j++){
                if(array[i] == uniqueArray[j]){
                    isExist = true;
                    break;
                }
                else{
                    isExist = false;
                }
            }
            if(isExist == false){
                uniqueArray[uniqueArray.length] = array[i];
            }
        }
        return uniqueArray;
    }
    
    0 讨论(0)
  • 2020-11-22 14:53

    Here's a much cleaner solution for ES6 that I see isn't included here. It uses the Set and the spread operator: ...

    var a = [1, 1, 2];
    
    [... new Set(a)]
    

    Which returns [1, 2]

    0 讨论(0)
  • 2020-11-22 14:55

    If you don't need to worry so much about older browsers, this is exactly what Sets are designed for.

    The Set object lets you store unique values of any type, whether primitive values or object references.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

    const set1 = new Set([1, 2, 3, 4, 5, 1]);
    // returns Set(5) {1, 2, 3, 4, 5}
    
    0 讨论(0)
提交回复
热议问题