0

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>
4
  • Why don't you remove them before shuffling? Commented Aug 28, 2012 at 19:32
  • @MarkF Please choose a best answer by clicking on the check mark next to it. It helps a lot. Commented Aug 28, 2012 at 22:39
  • I still can not get it to remove the duplicates. Can you put your code inside my script to make sure I am putting it inside my script correctly? Commented Aug 28, 2012 at 23:56
  • @MarkF Please choose a best answer by clicking on the checkmark next to it. It helps a lot. Commented Aug 31, 2012 at 22:30

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to use all the numbers. They are weighted.
@MarkF: I've updated my answer, you can use unique() to remove duplicates.
2
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]

3 Comments

Did you seriously just minify @João's answer?
No. I minified my own. I know it has the same function name and everything. Should I change it?
No, don't worry, just wondering (they compile to the same thing under Google Closure Compiler).
1

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

More about the Set object: Mozilla - Set object

Comments

0

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>

2 Comments

you're right, jQuery is just so ubiquitous at this point, I figure most folks will be using it + will recognize the $ when they see it... but this isn't always the case (nor does $ === jQuery) ... so I shouldn't assume, thnx for the edit.
How do I call the function using document.write
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.