Coder Social home page Coder Social logo

Delete DynamicArray about algorithms101 HOT 8 CLOSED

rasmus4200 avatar rasmus4200 commented on May 29, 2024
Delete DynamicArray

from algorithms101.

Comments (8)

jrasmusson avatar jrasmusson commented on May 29, 2024

Hi André! Thank you so much for commenting on this code.

I think the solution I have works as is.

public void delete(int index) {
        // Copy down
        for (int j = index; j < size - 1; j++) {
            data[j] = data[j + 1];
        }
        size--;

        // Clear if last element in array
        if (index == size) {
            data[index] = null;
        }
    }

Because I decrement size-- before doing the check, I can delete the last element OK.

So if we look at this example.

0 1 2 size = 3
a b c

delete(2)
copy down

0 1 2 size = 3
b c c index = 2

We now have an extra c at index = 2 that we want to delete.
If we decrement the size--

0 1 2 size = 2
b c c index = 2

We can delete the last element if the index == the new size or our array.

if (index == size) {   // 2 = 2
  data[index] = null;
}

0 1 2 size = 2
b c null index = 2

Does this make sense? I may have missed something, but I think it works.

If it doesn't, can your write a unit test that shows me where the bug is? I can then try to fix.

Thank you so much for raising this question and asking.

Cheers - Jonathan

from algorithms101.

andrelugomes avatar andrelugomes commented on May 29, 2024

Hi Jonathan, tanks fos answer :)

I'm actually writing unit tests for the exercises

https://github.com/andrelugomes/data-structures-and-algorithms/blob/master/java/src/test/java/datastructures/DynamicArrayTest.java

I'll follow your suggestion and check by the tests.

from algorithms101.

andrelugomes avatar andrelugomes commented on May 29, 2024

It doesn't work.

public void delete(int index) {
        // Copy down
        for (int j = index; j < size - 1; j++) {
            data[j] = data[j + 1];
        }
        size--;

        // Clear if last element in array
        if (index == size) {
            data[index] = null;
        }
    }
@Test
    public void shouldDeleteFromArray(){
        var array = new DynamicArray<>(2);
        array.add("object1");
        array.add("object2");
        array.add("object3");

        array.delete(1);

        assertThat(array.size(), is(2));
        assertThat(array.get(0), is("object1"));
    }

image

By my sugestion:

image

from algorithms101.

jrasmusson avatar jrasmusson commented on May 29, 2024

Which assertion in your unit test is failing?

from algorithms101.

andrelugomes avatar andrelugomes commented on May 29, 2024

The unit tests are passing. But the final array still holding dirty data in last index.

from algorithms101.

jrasmusson avatar jrasmusson commented on May 29, 2024

Apologies :) I understand now. Bad unit test on my part.
Yes you are correct! And you solution works! Thank you.

I am going to tweak it a bit and add some comments.

Thanks again for pointing this out. You are really getting this stuff!

Cheers - Jonathan

from algorithms101.

jrasmusson avatar jrasmusson commented on May 29, 2024

Note: I gave you credit for finding the but on the page that explains what the bug is. Let me know if you if that is OK.

Cheers - Jonathan

from algorithms101.

andrelugomes avatar andrelugomes commented on May 29, 2024

Hi @jrasmusson

I'm pretty happy to discuss with you that solution.
Congratulations! The course is awesome.

from algorithms101.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.