Ad
  • Default User Avatar
  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    i wanted to do this but couldnt figure the logic out. nice solution tho

  • Custom User Avatar

    this was my solution!

  • Custom User Avatar

    Pienso que tu solucion se podria mejorar haciendo una copia de a en vez de modificarla

  • Default User Avatar

    This is pretty similar to my solution but using forEach instead of a for loop. I like how simple your solution is though, espeically with the concatenation.

  • Custom User Avatar

    Ugly as heck. Why on earth would you omit white space? Why would you want your solution to be extra cryptic and extra difficult to read? Not even going to critique beyond "this is hard to look at"...

  • Custom User Avatar

    Not good at all, it's O(n²)

  • Custom User Avatar

    This helped me as well. Thank you for the explanation. I ended scrapping the the use of splice() simply becuase i couldnt figure that out. ended up just deleting the element in the list using delete and using filter to filter out falsy values that were not === 0 till i got what was needed. This was helpful to understand though. Thanks again

  • Default User Avatar

    I think it's bad practice to use a ArrayList with int. Since each time the ArrayList is accessed, the packaging is unpacked by a variable to the object type.

  • Custom User Avatar

    It is clear that the i-- is needed from the test case where a=[1,2,2] and b=[2]. In this case array.a has a length of 3 and the function will loop through array.a checking for a match with array.b at index 0, then index 1, then index 2. So what happens is the loop finds no match when it checks against index 0 (because a[0]=1), then the loop finds the first match when it checks against index 1 (because a[1]=2) and splice removes this value. However doing so modifies the length of array.a! array.a now looks like a=[1,2] which has a length of 2, comprising index 0 and index 1. But your loop has already checked index 0 and index 1, so the loop will now terminate. By removing the value of a[1] in your loop you have now moved the value which was at a[2] into the position of a[1] in the array. And now you need to make sure you loop checks this new value. So you have to reduce the loop count by 1 (i--) so that it will check the same index in the array again to see if the new value is also a match.

    tl;dr Without the i-- (which just decrements/decreases the value of i by 1) what happens is you loop through array.a and it finds a match at a[1], moves value a[2] into a[1] and does not check a[1] again. You need it to perform this check to be correct.

  • Default User Avatar

    I kind of like this way. I got a takeaway here.

  • Default User Avatar

    if the index is -1, it means that its not found in the searched string

  • Default User Avatar

    After struggling for a while I looked at your solution. Mine was exactly the same except I was missing 'i--' and so couldn't delete all instances of b from a. Still not sure I understand how 'i--' allows you to remove them all. Could you explain? Thanks

  • Loading more items...