Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Razputin · Jun 15, 2020 at 06:00 PM · arraylistindex

Find specific element when duplicates exist in list.

How do you get an element from a list when there are duplicates of that element in the list?

Take the following list for example.

A, B, C, C, C, D

IndexOf will always return the C in element 2, but what if I want C in element 4?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ShadyProductions · Jun 15, 2020 at 06:56 PM

So the way I'm taking your question is that you are comparing some default value type of c# that don't have a hashcode comparison like objects do

Say you have an array of integers:

 var arr = new int [] {1,1,2,3,5,6,6,7,7,7,9};

And you want to find nr 7 on index 8

There are a few ways:

You already know that there is a 7 on index 8 and access it directly arr[8]

Or

Loop the array and count up the 7's until you reach the 2nd one:

 int position = 0;
 foreach (var nr in arr)
 {
     if (nr == 7)
     {
         position++;
         if (position == 2)
             return nr;
     }
 }

Your question would make a lot more sense if you had an object that you can uniquely identify with some kind of an Id, or hash.

 public class Book
 {
     public string Author;
     public string Title;
 }

An example:

 public Book[] Books = new Book[2];
 var exampleBook = new Book { Author = "Idk", Title "Example" };
 Books[0] = new Book { Author = "Me", Title = "Also me" };
 Books[1] = exampleBook;

 var index = Books.IndexOf(exampleBook);

IndexOf here will return the correct index 1, because it compares the hashcode of the two object and they are the same. You can override this behaviour, by overriding the Equals method.

You can read about it here:

https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netcore-3.1

Comment
Add comment · Show 9 · 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 Razputin · Jun 15, 2020 at 07:07 PM 0
Share

Hi thanks for the answer,

The issue is that I don't always know which element is where in the list because they can be moved by the player.

It's an inventory system.

Say we have axe in slot 1, nothing in slots 2-9.

If the player drops the axe into slot 9 it will ins$$anonymous$$d get dropped into slot 2 because i use a blank "Nothing" item to fill empty slots in the list.

Video to help try and explain. https://www.youtube.com/watch?v=_AonRL0stp4&feature=youtu.be

avatar image Razputin Razputin · Jun 15, 2020 at 07:13 PM 0
Share

Actually ins$$anonymous$$d of using the default "Nothing" item, if i Item i = new Item(); and then add I to the list they are technically unique and it doesn't cause this issue.

Thanks for the assistance.

avatar image ShadyProductions Razputin · Jun 15, 2020 at 07:15 PM 0
Share

Why not set the inventory slots you don't use to null?

Show more comments

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

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

For all elements above a certain index 1 Answer

How do I return the index of an array of sprites as an int? 1 Answer

How do I check a List[0], when it's nothing? 1 Answer

What type of array should I use to handle a convoy? 0 Answers

List gives "Array index is out of range" for no reason 2 Answers


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