loops - How do I perform a duplicate check in code? -
this might no-brainer some, i'm trying check if there duplicate values in code.
to clearer, creating 5 variable integers randomizes number once created. let's they're named i1
, i2
, i3
, i4
, i5
.
i want run loop check on each other make sure don't have possible duplicates. if do, i'll re-random second integer that's being checked. (e.g if (i1 == i4) { i4.rand(); }
) that's make sure i1
doesn't need re-checked against checked values or being stuck in long loop until different number found.
this i'm thinking if 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 can "manually" creating lots of if / else
statements, there better way it? isn't feasible if increase integer limit 20 , have if / else
way through 20 value checks. know there is, can't remember. search on google turning nothing (maybe i'm searching wrong keywords), why i'm asking on here @ stackoverflow.
all want know how do it, theory-wise (how check duplicates in theory?). answer doesn't need workable function.
if want create demo code using programming language i'm using problem, itsexcel vba
. think information able apply theory-wise lot of other programming languages, feel free write in javascript/jquery
, c++
, c#
, etc. remember comment!
you looking set;
set<integer> hs = new hashset<integer>(); hs.add(i1); if(!hs.add(i2)){ randomize(i2); }
hope helps. let me know, if have questions. above concept of do.
to logic code, be
set<integer> hs = new hashset<integer>(); for(int count=0; count<array.length; count++){ // store data array , loop datatoinsert = array[count]; while(hs.add(datatoinsert)){ datatoinsert = randomize(datatoinsert); } }
Comments
Post a Comment