answersLogoWhite

0

There are several ways to either confirm that a given number is unique, or confirm that it is a duplicate of some number already on a list.

Unique on a short list

1) Compare the given number to each and every number on the list, one at a time, to see if the given number is already on the list. This is the best method for a short list of numbers.

Unique on a long list

2) If you are building a very long list of numbers, and you want to make sure that every number on the list is unique, sort the initial list of numbers and then do a binary search to see if the given number is already on the list. If it's not already there, then insert the new number in the appropriate place that keeps the list sorted.

3) If you already have a very long list of numbers, and you want to see if each and every number on that list is unique, sort the list and then scan the sorted list for duplicates (which will be consecutive). This is quick and easy to do with the "sort" and "uniq" command-line tools.

To generate a new list that only has the *unique* items in the original list (if any item is duplicated, that item is entirely left out of the second list):

cat in.txt | sort | uniq -u | tee uniques.txt

To generate a new list that lists only the *duplicated* items in the original list (but every item in the new list is unique in that new list, because it leaves out the second, third, etc. duplicates of the repeated items):

cat in.txt | sort | uniq -d | tee duplicates.txt

If that command doesn't print out anything, you can be sure that every item in the original list was unique.

To generate a new list that has both the *unique* items *and* the *duplicated* items -- i.e., *every* item in the original list exactly once (leaving out the second, third, etc. duplicates of repeated items), so every item in the new list is unique in that new list:

cat in.txt | sort -u | tee uniquified.txt

Universally unique

4) Sometimes you want a number that is globally unique, that no other human has ever seen before.

It's not possible to directly compare some given number with every number that any human has ever seen before If someone gives you a number, it's usually not possible to check after-the-fact if this is a unique number that only you two have seen, or if that someone gave you a copy of some old number that has been well-known by dozens of other people for decades.

However, it is possible to build a process that generates "new" numbers that are almost certainly unique, such that it is practically impossible that any human has ever seen it before.

(Such processes often involve "/dev/random", hardware random number generators, cryptographically secure pseudorandom number generators, or some combination of them).

Such processes are used to generate a universally unique identifier (UUID),

such as a globally unique identifier (GUID).

Generating such numbers is quick and easy to do with the "uuidgen" command-line tool (spelled "uuid_generate" on some systems).

uuidgen | tee unique_number.txt

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine

Add your answer:

Earn +20 pts
Q: How do you check whether the number is unique number?
Write your answer...
Submit
Still have questions?
magnify glass
imp