Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thalnus · Mar 18, 2017 at 04:08 PM · listsremove

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 18, 2017 at 05:53 PM 1
Share

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.

avatar image thalnus · Mar 19, 2017 at 08:48 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }
     }
 
 }
 


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges