- Home /
Removing the last duplicate of an object in List
I have a List with possible duplicates. I need to remove the last instance of a certain object in the list, but .Remove always removes the first instance. How can I achieve this without knowing the index of the last instance?
Just for the sake of clarity, I do not need to remove the last object in the list, but the last instance of a given object in the list. Example: List< GameObject> list = new List< GameObject>() { obj1, obj2, obj3, obj1, obj4 }
I need to remove the second instance of obj1, being list[3]...but I won't know what its index is.
Often you can solve a problem by looking at the right collection. It could be that you want to use a HashTable. In this case, the second instance would not make it to the collection as there would already be one of it.
hashTable.Add(objA);
bool result = hashTable.Add(objA);
In this case, you have one iteration of objA and result is false to indicate the reference was not added in the second case.
The best way I've found is to reverse the list, then remove the object, then reverse the list again to return it to the original order.
Answer by gjf · Mar 18, 2017 at 04:07 PM
try this - it creates a new list of unique entries.
var uniqueList = list.Distinct().ToList();
you'll need to make sure you have this too:
using System.Linq;
Answer by Azrapse · Mar 18, 2017 at 10:13 PM
If for whatever reason you cannot use the solutions that others have suggested to you (no HashTable because you need to track order in the collection, no Distinct because you actually want to keep duplicates of other objects), you could do something like this:
var index = list.LastIndexOf(obj1);
list.RemoveAt(index);
That assuming that you want to remove obj1 even if there is only one of it. If you aren't sure, and you want to remove it only if there is more than one, you will need to check that first.
Answer by ExtinctSpecie · Mar 19, 2017 at 11:55 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Temporary : MonoBehaviour {
public List<string> strs;
void Start()
{
string[] tempArray = { "a", "b", "c", "a", "c" , "c" };
strs = new List<string>();
strs.AddRange(tempArray);
//so we want to remove the last "c"
PrintList ();
RemoveLastDublicate (ref strs);
PrintNewList ();
}
void RemoveLastDublicate(ref List<string> list)
{
List<string> uniqueOccurrences = new List<string>();
int lastDubIndex = -1;
for (int i = 0; i < list.Count ; i++)
{
string str = list [i];
if (!uniqueOccurrences.Contains (str))
{
uniqueOccurrences.Add (str);
}
else
{
lastDubIndex = i;
}
}
if(lastDubIndex != -1 )
list.RemoveAt(lastDubIndex);
}
void PrintList()
{
print ("list");
foreach (string str in strs)
{
print (str);
}
}
void PrintNewList()
{
print ("new list");
foreach (string str in strs)
{
print (str);
}
}
}
Your answer

Follow this Question
Related Questions
UNET Networking, syncronizing lists question 1 Answer
MissingReferenceException? 1 Answer
Remove and Destroy Instantiated object in list 1 Answer
Unable to remove GameObjects from list 4 Answers
can add to a list but not remove 1 Answer