0
var data = localStorage.getItem('oldData').split(" ");

I am accessing localStorage as above and getting an array of values. Some of the elements are repeated in the string value for oldData, for example:

apples oranges apples apples

I want data to have only two elements apples and oranges. How can I do this in Javascript?

raidfive
  • 6,603
  • 1
  • 35
  • 32
Lazer
  • 90,700
  • 113
  • 281
  • 364

5 Answers5

2
Array.prototype.unique = function(){
  return Object.keys(this.reduce(function(r,v){
    return r[v]=1,r;
  },{}));
}

Strap it on. It's O(n) because using an object just requires you to loop through the array once and assign every value in it as a key, overwriting as you go. This only works when the values are primitives (or you have Harmony WeakMaps). But that's almost always the kind of array you want to do this one so it works out.

For bonus points here's the second best way to do it. This is at minimum twice as fast as the normal double loop answers and is at minimum as good as the ones requiring presorting, (but still worse than the above hash method which is infinitely faster).

Array.prototype.unique = function(){
  return this.filter(function(s, i, a){
    return i == a.lastIndexOf(s);
  });
}

The reason it beats every other answer aside from the hash is because it's able to get the benefit of sorting without doing the sorting step. It only searches from the current item forward, and from the other end in reverse, so there will never be a case where two items are checked against each other twice, and there will never be an unnecessary comparison done because it always quits at the very minimum amount of work needed to make a final decision. And it does all of this with the minimum possible creation of placeholder variables as a bonus.

0

first is to insert one value in your array by using push

var array = [];
array.push("newvalue");

then the next insertion of value, check if your value is existing in your array using "for loop". then if the value does not exist, insert that value using push() again

Netorica
  • 18,523
  • 17
  • 73
  • 108
0
Array.prototype.unique = function()
{
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++)
    {
        for(var j=i+1; j<l; j++)
        { if (this[i] === this[j]) j = ++i; }
        a.push(this[i]);
    }
    return a;
};
0

Something like this should do the trick:

uniqueValues = function(array) {
    var i, value,
    l = array.length
    set = {}, 
    copy = [];

    for (i=0; i<l; ++i) {
        set[array[i]] = true;
    }

    for (value in set) {
        if (set.hasOwnProperty(value)) {
            copy.push(value);
        }
    }

    return copy;
 }
Tom
  • 43,583
  • 4
  • 41
  • 61
0

This is what I have used finally

var data = localStorage.getItem('oldData').split(" ");

var sdata = data.sort();
var udata = [];
var j = 0;
udata.push(sdata[0]);
for (var i = 1; i < data.length - 1; i += 1) {
    if (sdata[i] != udata[j]) {
        udata.push(sdata[i]);
        j++;
    }
}
Lazer
  • 90,700
  • 113
  • 281
  • 364