Remove doubles from an array

I’ve got an array, which is filled with random numbers. I want to remove all numbers which occur more than once. I tried to do it like this, but it didn’t help. It just removed one of each set of number (for example if the data was 5,5,5 , it removes one 5 and leaves the other two.) Updated with new code, still some problems, any suggestions?

function check(){

   for(var d =0; d<=newPrimes.length; d++){
     a=d;
     b=d++;
     if(newPrimes[a] == newPrimes[b){
      newPrimes.RemoveAt**;**

** d–;**
}
}
print(“List of Primes!” + newPrimes);
** print(“Took” + Time.realtimeSinceStartup);**
}

Do you have to use an array? If you are comparing a very large number of unsorted values, some kind of tree structure could be much more efficient.

So implement a binary tree, add elements to the tree, if a prime is already in the tree do not re-add it.

After creating the tree, you could then flatten it to an array.

This should be dramatically more efficient than checking if an Array contains a given value.

e.g. in pseudocode

if !binaryTree.Contains(prime) binaryTree.Add(prime) //binary tree contains is faster than array contains

The task that you're trying to achieve works computationally well if the array is sorted. In fact, the code that you've posted is intended to be executed after the array has been sorted. There's more: if it removes one "5" out of three, is just a coincidence, and that's certainly caused by the presence of two adjacent 5s. If all the three 5s were "scattered" in position, it'll removes no one. To explain better:

             1 3 4 5 5 6 7 5

In the situation above it will remove one 5.

             1 5 3 4 5 6 7 5

Here, it removes nothing.

Use an algorithm like quicksort to sort your array before using your code. Or use an algorithm like that - that works also for non-ordered arrays - , with two inner loops (it's just a draft of mine, hope that it works):

function check(){
var delete = false;
var index = 0;
   for(var a=0; a<=newPrimes.length; a++){
      for(var b =0; b<=newPrimes.length; b++){
         if (a!=b && newPrimes[a]==newPrimes**){**
 **delete=true;**
 **index=b;** 
 **b=newPrimes.length;  //this exits the inner cycle**
 **}**
 **}**
 **if (!delete){**
 **newArray.Add(newPrimes[index]);**
 **}**
 **else delete=false;**
 **}**
**print("List of Primes!" + newArray);**
**print("Took" + Time.realtimeSinceStartup);**
**}**
**```**
**<p>EDIT: Note also that the code that you've posted isn't <em>exactly</em> intended for your purpose: it removes all the duplicate items, not all items that appear more times. In the former, if you have three 5s, it will leave one of 'em. In the latter, it will remove ALL of them, and I think that you're interested in this one. </p>**

You need to scan repeatedly down the array. Compare the first with all the ones after, then compare the second with all after etc. The current code only has one loop and only compares the indexes rather than the contents of the array at these points. a and b will always be different as b is just one bigger than a.
You need two loops. Also take care as when you remove one from the array the length of the array will change and you will need to retest the same index as the list is shifted down from the removal.

Using the Linq libray it would be as simple as calling Distinct().

Ex:

newPrimes = newPrimes.Distinct();

http://msdn.microsoft.com/en-us/library/bb348436.aspx