I've thought of using a if statement to say if that number has already been produced into my line change the number which it was going to produce into a different number other then that, but I must of wrote it incorrect since it does not work.
-
2Welcome to Stack Overflow! Nobody can tell you what's wrong with code we can't see. Please provide a [mcve] demonstrating the problem. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Dec 24 '21 at 14:54
1 Answers
Let's consider a sequence of random hex "digits" :
f 5 3 0 0 5 e 8 e 8 5 6 f
A naïve approach would try to check if the previous number is equal to the next in the sequence and discard it.
That will not work since only a small subset of the duplicated numbers will be come in direct sequence.
What you will need is to keep a track of the ALL numbers already present.
if you store your final sequence in an array you could check for each element, as long as the number of the desired elements is low.
For example :
f (first elements nothing to check agains, add it)
f 5 (check against index 0-0 , f, not present add it)
f 5 3 (check against index 0-1 , 3, not present add it)
f 5 3 0 (check against index 0-2 , 0, not present add it)
f 5 3 0 0 (check against index 0-3 , 0 , found - skip it)
f 5 3 0 5 (check against index 0-3 , 5 , found - skip it)
f 5 3 0
8 (check against index 0-3 , 8, not present add it)
f 5 3 0 8 e (check against index 0-4 , e, not present add it)
f 5 3 0 8 e
and so on. This is basically what a human eye would do, if you cannot keep all the numbers in your head.
If the sequence is large scanning the output array will quickly become too inefficient and slow.
There ways to optimize the check by using hash sets/maps.(see Remove duplicate values from JS array and Selecting Unique Elements From a List in C# )
If the number of possible elements is low, you might want a "shuffle" to get your output (see Random shuffling of an array).

- 833
- 6
- 18