1

Following is the scanerio.

  1. How can we generate a unique id (string/number), from a given unordered collection/set of objects (having their own uids).

  2. How to make sure the generated id will always be the same even if the order of the objects changes in the collection/set.

I am working with groups of objects and each group needs a uid, to represent certain objects are present in it regardless of the order of the object, hope it makes sence, thanks in advance for your help.

5
  • What is your approach on this? Commented Jan 23, 2018 at 5:23
  • Lets say you have an object A and it is composed of different objects (collection). To check if the object A contains a certain object you will have to itterate the entire collection but with uid you can just check the uid to be the same as required. Commented Jan 23, 2018 at 5:49
  • Is there an (almost) unlimited number of possible objects that can be in the collection (for example, each object represents a random String)? Or is it limited (for example, each object represents a country, and only 200 or so of those exist in the world)? Commented Jan 23, 2018 at 5:53
  • No the number of objects in the collection are limited ~20 and each object is unique with its own uid. Commented Jan 23, 2018 at 6:02
  • Lets say you have 5 total objects as ob1, ob2, ob3, ob4, ob5 and you create objects as O1{ ob1,ob2} and O2{ob2,ob4,ob1} and O3{ob2,ob1}. Is there a way to generate a uid for the collections of objects ob1to ob5, keeping in mind the id generated has to be same for O1 and O3 as both have same collection? Commented Jan 23, 2018 at 6:12

1 Answer 1

1

Given that the number of possible objects are so limited, you can easily do this. Give each object a value which is a power of 2 (1,2,4,8,16, etc.) Then, for the collection, just sum the values of the objects in the collection. The result should fit in a 32 bit integer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.