0

How can I compare the n-1 elements of two Sets ?

The algorithm I want is one which take two SortedSet in argument, if their size is equal and if their n-1 element are identical, then we merge them. https://code.sololearn.com/cTjYUZgWci6w/?ref=app

11th Nov 2020, 3:06 PM
Adrien Linares
Adrien Linares - avatar
9 Réponses
+ 2
In the same code before returning the merged set you can store it inside another SortedSet and check if the size returned is equal to (set1.size()+set2.size() -1). If that is equal then you can return the merged set otherwise return null. Edit: Adrien Linares You must store that equation value before merging.
11th Nov 2020, 4:27 PM
Avinesh
Avinesh - avatar
+ 1
What type is BooleanVariable here?
11th Nov 2020, 3:30 PM
Avinesh
Avinesh - avatar
+ 1
This code should work exactly for the conditions you have provided. I have done it for strings but the approach would be the same for all types. https://code.sololearn.com/ccmH2o9LXMQB/?ref=app
11th Nov 2020, 4:03 PM
Avinesh
Avinesh - avatar
+ 1
Is that working?
12th Nov 2020, 6:00 AM
Avinesh
Avinesh - avatar
0
Avinesh It is a class which which take in parameter a String
11th Nov 2020, 3:31 PM
Adrien Linares
Adrien Linares - avatar
0
It looks good thank you! there's a last condition, it's that the n other elements are different
11th Nov 2020, 4:12 PM
Adrien Linares
Adrien Linares - avatar
0
Btw I also thought to convert the set into list
11th Nov 2020, 4:12 PM
Adrien Linares
Adrien Linares - avatar
0
Avinesh these three conditions otherwise we return null
11th Nov 2020, 4:22 PM
Adrien Linares
Adrien Linares - avatar
0
Okey I modified it, is it good now?
11th Nov 2020, 4:37 PM
Adrien Linares
Adrien Linares - avatar