Mark F
Mark F

Reputation: 1

Array Remove Duplicate Results Javascript

I need to remove the duplicates after they are shuffled. Currently the results come out with duplicates.

Example: Results 2,2,1,4,4,3,5,5, I need as 2,1,4,3,5

This is a large array

<script>
Array.prototype.shuffle = function() {
var input = this;

for (var i = input.length-1; i >=0; i--) {

    var randomIndex = Math.floor(Math.random()*(i+1)); 
    var itemAtIndex = input[randomIndex]; 

    input[randomIndex] = input[i]; 
    input[i] = itemAtIndex;

}
return input;
}

var tempArray = [ 

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,

3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,

4,4,4,4,4,4,4,4,4,4,4,4,4,

5,5,5,5,5,
]

tempArray.shuffle();

document.write(tempArray);

</script>

Upvotes: 0

Views: 1369

Answers (5)

Dan Porat
Dan Porat

Reputation: 436

You can use the following approach, using ES6 syntax: const unique = [...new Set(array)]

More about the Set object: Mozilla - Set object

Upvotes: 1

Narayanan R
Narayanan R

Reputation: 31

One really quick way of removing duplicates from any JavaScript array is the following:

var listWithDuplicates = [1, 2, 2, 4, 3, 4, 5, 3, 1, 5];
console.log(listWithDuplicates.filter(function(element, index, array) {
  // using lastIndexOf(...) retains the last
  // repetition preserving the order of the array
  return index === array.indexOf(element);
}));
//prints out: [1, 2, 4, 3, 5] - with indexOf variant
//prints out: [2, 4, 3, 1, 5] - with lastIndexOf variant

Hope this comes in handy for some purpose related to removing duplicates from JavaScript arrays.

Please share feedback about any corner cases not addressed by this solution or any suggestions for improving this answer.

Upvotes: 0

David G
David G

Reputation: 96875

function unique(b) {
    for (var c = [], d = {}, a = 0; a < b.length; a++) {
        d[b[a]] || (c.push(b[a]), d[b[a]] = !0);
    }
    return c;
}

unique( [1, 1, 1, 2, 2, 3] ); // [1, 2, 3]

Upvotes: 2

Nick Briz
Nick Briz

Reputation: 1977

If you have access to jQuery, you could split the process into two arrays, take that result, loop through it and only add it to the newArray if it's not already in there. This way they come out in the same order.

var someArray = [3,3,3,3,3,3,4,4,4,4,1,1,1,1,1,2,2];

function cleanArray(oldArray) {
    var newArray = [];
    for(i=0;i<oldArray.length;i++) {
        if( $.inArray(oldArray[i], newArray) == -1 ) {
            newArray.push(oldArray[i]);         
        }
    }
    return newArray;
}

document.write(cleanArray(someArray));
//result would be 3,4,1,2

EDIT: I've updated the function so it works the way I believe you imagined. Here's a working example: http://jsfiddle.net/xe2F8/

Also, don't forget to link to jquery:

    <script src="http://code.jquery.com/jquery-latest.js"></script>

Upvotes: 0

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91379

Instead of using that large array, simply use [1,2,3,4,5].shuffle(). This way, you won't get duplicates. But here's a function that will give you a unique array, i.e., an array without duplicates:

function unique(arr) {
  var result = [],
      map = {};
  for (var i = 0; i < arr.length; i++) {
    var duplicate = map[arr[i]];
    if (!duplicate) {
        result.push(arr[i]);
        map[arr[i]] = true;
    }
  }
  return result;
}

Then, just use unique(tempArray.shuffle()).

Here's a DEMO.

Upvotes: 3

Related Questions