This might be a no-brainer to some, but I'm trying to check if there are any duplicate values in my code.
To be clearer, I am creating 5 variable Integers that randomizes a number once they are created. Let's say they're named i1
, i2
, i3
, i4
, i5
.
I want to run a loop to check on each other to make sure they don't have any possible duplicates. If they do, I'll re-random the second Integer that's being checked. (e.g if (i1 == i4) { i4.rand(); }
) That's to make sure i1
doesn't need to get re-checked against all the previously checked values or being stuck in a long loop until a different number is found.
This is what I'm thinking if it was an entire if else statement : if (i1 == i2)
, if (i1 == i3)
, if (i1 == i4)
, if (i1 == i5)
, if (i2 == i3)
, if (i2 == i4)
, if (i2 == i5)
, if (i3 == i4)
, if (i3 == i5)
, if (i4 == i5)
I know I can probably do it "manually" by creating lots of if / else
statements, but is there a better way to do it? It probably isn't very feasible if I increase my Integer limit to 20 and I have to if / else
my way through 20 value checks. I know there is, but I just can't remember. Search on Google is turning up nothing (maybe I'm searching for the wrong keywords), which is why I'm asking over here at StackOverflow.
All I want to know is how do I do it, theory-wise (how would you check for duplicates in theory?). The answer doesn't necessarily need to be a workable function.
If you want to create a demo code using the programming language I'm using for this problem, itsExcel VBA
. But I think this information would be able to apply theory-wise to a lot of other programming languages, so feel free to write in javascript/jQuery
, C++
, C#
, etc. Just remember to comment!