0
public void CheckForHighScore(){
  finall = PlayerPrefs.GetInt("score");// taking score variable from another script to pass in ScoreBoard

   for (int x = 0; x < highScores.Length; x++){
     if (finall > highScoreValues[x]){ 
       for (int y = highScores.Length - 1; y > x; y--) { //sorting
        highScoreValues[y] = highScoreValues[y - 1];
       }
       highScoreValues[x] = finall;
       DrawScores();
       SaveScore();
       PlayerPrefs.DeleteKey("score");
            break;
        }
    }
}



void DrawScores() { 
    for (int x = 0; x < highScores.Length; x++) {  // just adding score to string
        highScores[x].text = highScoreValues[x].ToString();

    }
}

Hey guys. How can I remove duplicates from ScoreBoard array? Tried myself to put this code below the sorting loop but actually nothing happens. I've also tried other methods but they don't work properly. Any help is appreciated.


/*for (int j = 1; j < x; j++)
        {
            if (highScoreValues[j] == highScoreValues[x])
                break;
            else
                highScoreValues[x] = finall;
        }*/
Gigazelle
  • 1,424
  • 1
  • 24
  • 34
Vita
  • 13
  • 4
  • Can you use Linq? If `highscores` is an array of a numeric type, you could do `var sorted = highscores.OrderByDescending(s => s).Distinct().ToArray();` – Eric J. Dec 09 '18 at 21:37
  • Tried linq like one million times but it doesn't work not even sure if I am not too dumb to use it properly – Vita Dec 09 '18 at 21:48

2 Answers2

0

You can use linq. If highscore is an object, you must override Equals and GetHashCode

        public override bool Equals(object obj)
        {
            // Insert the Code to override
        }
        public override int GetHashCode()
        {
            // insert the Code to override
        }

Be sure, that GetHashCode returns a unique integer for each object. For this way you can see more here: Correct way to override Equals() and GetHashCode()

If highScore is a primitives, you don't need to override the methods.

After you have unify highScore, you can use this:

var sorted = highScores.OrderByDescending(h => h).Distinct().ToArray();

Kostarsus
  • 37
  • 5
  • "Be sure, that GetHashCode returns a unique integer for each object" - No, it doesn't need to. It's good if it does, but it's not a necessity. – Enigmativity Dec 09 '18 at 23:58
-1
List<*Insert type of score here*> cache = new List<*Insert type of score here*>();
for(int index = 0; index < highScores.Length; ++index)
{
    if(cache.Contains(highScores[index]))
    {
        highScores.RemoveAt(index);
        --index; // Every time you have to remove an item, it will slot the next item
                 // into that index and subtract 1 from highScores length. Thus you 
                 // will have to decriment the index in order to check the item that 
                 // gets pushed into that index of your highScore Array. You many 
                 // need to check the syntax if highScores is not a List. But 
                 // hopefully this helps :)
    }
    else 
    {
        cache.Add(highScores[index]);
    }
}
    }
}
Quinn H
  • 1
  • 1