'how do i compare four randomly geenrated codes and find how many are similiar
I have a function that generates and saves numbers in 4 variables, following which I need to compare them and find how many are similar? I was thinking of using the || and &&, but I am struggling to figure out how to tell if the three variables are the same.
//when all are same
if(w1 == w2 && w3 == w4 && w1 == w4) {
//output a result
}
//if three are same
else if(w1 == w2 && w2 == w3 || w4 == w3 && w3 == w2 ||?? ){
//output a result}
//if two are same
else if(w1 == w2 && w2 == w3 || w4 == w3 && w3 == w2) {
//output a result
}
//if none are the same
else {
//output a result
}
Solution 1:[1]
Put these four values in a four-element array. Sort the array (as shown here Sort 4 number with few comparisons). Loop (or just use straight-line code) through the first three elements, comparing each element to the next, and increment a counter each time a comparison shows they are equal. The counter's value is interpreted as the following:
0 All are different.
1 Two are the same.
2 Three are the same, or the first two and last two are the same.
3 All four are the same.
To distinguish between the two cases when counter is 2, compare the second and third values. If they are not equal, the first two are equal and the last two are equal.
Solution 2:[2]
Streams
Let's use an IntStream produced from ThreadLocalRandm#ints to produce some random integers as inputs.
List < Integer > integers =
ThreadLocalRandom // A random number generator automatically instantiated once per thread.
.current() // Obtain the generator for this particular thread.
.ints( 0 , 3 ) // Produce an `IntStream` of randomly generated `int` primitive values. Specify the lowest (inclusive) and the highest (exclusive) of the range of values to generate.
.limit( 7 ) // Generate seven of those `int` values.
.boxed() // Convert from `int` primitives to `Integer` objects.
.toList(); // Collect our generated `Integer` objects into a list.
integers = [0, 2, 1, 1, 0, 1, 1]
Let's look for duplicates.
Make a stream of our list of Integer objects. For each of those Integer objects, examine our list for how many times that object (or an object matching per .equals method) appears. Conveniently, we can call Collections.frequency to determine that count. Filter out those that appear only once, while keeping those that appear more than once.
Collect the hits into a Set. A set in Java is distinct, automatically forbidding duplicates.
Set < Integer > dups =
integers
.stream()
.filter( ( Integer i ) -> Collections.frequency( integers , i ) > 1 )
.collect( Collectors.toSet() );
dups = [0, 1]
This code could be optimized. We can eliminate duplicates from our stream of integers to avoid redundant calls to Collections.frequency. Just add a call to Stream#distinct.
Set < Integer > dups =
integers
.stream()
.distinct()
.filter( ( Integer i ) -> Collections.frequency( integers , i ) > 1 )
.collect( Collectors.toSet() );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user7291 |
| Solution 2 |
