Skip to content
Advertisement

Sum of intervals in Java

I am stuck on this kata in CodeWars and I have tried for a long time, like for a week or so for one kata. If you want a description of my question, go here.

This is my code, if you believe me, there are lots of bugs in it. Can someone please help me out?

JavaScript

Advertisement

Answer

I think you are over-complicating the solution in general (which makes bugs harder to find), it seems that all that is really required is that you do not add duplicates in a Collection, and output the size of the collection afterward.

I whipped up a quick version of this (I only tried it out against 2 of the tests).

JavaScript

Output:

19

This solution first iterates against the outer array to obtain each range, then uses those values to iterate between them and add each number into a List if the number is not already in the List, by utilizing .contains().

Finally it returns the size of the List that contains each non-duplicated number.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement