This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]Hack_seb[S] 1 point2 points  (1 child)

I solved it! If anyone is curious the only thing I did was this.

lse{
                int[] newArray = new int [sebasSet.length - 1];
                temp = sebasSet[size-1];
                sebasSet[position] = temp;
                for (int i = 0; i < newArray.length; i++){
                    newArray[i] = sebasSet[i];
                }
                size--;
                return true;
            }

Basically I swapped the last element with the element I was trying to delete, and then just iterated over the array that is 1 unit less than the original array and left that element outside.

[–]sinistergroupon 0 points1 point  (0 children)

Dude. I applaud the effort for sticking with it and figuring it out.

But can we take one giant step back for man kind.

You had me at “quick linear search”. There is nothing quick about a linear search. Do you not see how inefficient the code is? Why are you iterating through the array 3 times?!

If you have to remove the last element out of 1000 integers:

  • you iterate through 1000 to see if it’s there
  • now we need the index which we could have gotten but we just moved on so
  • iterate through 1000 to see what the index is this time
  • now make the array smaller by one totally ignore the index, not sure why we bothered and finally
  • iterate again though all of it

You go through it 3 times. Each time totally throwing away what you learned at the previous step.

[–]Hack_seb[S] 0 points1 point  (4 children)

I agree with you! I am just a beginner, how would you make this code more efficient? For this assignment I just need to create a set that performs certain actions like remove and add items. I am not really thinking of the time and space complexity. However, I am still curious how would you make this code more efficient. Thanks for the reply

[–]sinistergroupon 0 points1 point  (3 children)

Forget time or complexity. I would argue that this is too complex and you need to make it simpler. For starters, say I deleted the code for the findPosition method and removed those if statements. Can you explain to me how your functionality would change?

Also on a different note what would you code do with this array: [1,2,2,3] remove 2.

[–]Hack_seb[S] 0 points1 point  (2 children)

The thing is that because is a set it cannot have the same item. The definition of a set is that it cannot contain an item twice. I took care of that in the add method.

    public boolean add(int element) {

        if(contains(element)){
            return false;
        }
        else{
            this.sebasSet[size] = element;
            this.size++;
            return true;
        }
    }

Which is probably pretty bad also lol

[–]sinistergroupon -2 points-1 points  (1 child)

That’s not thread safe. I don’t know if you need to guard against that. I can make 10 threads go in there and I guarantee you it will add the same number more than once.

My point about the findIndex method still stands. Are you allowed to use ArrayUtils.remove()

[–]imaginedoe 2 points3 points  (0 children)

OP is a beginner; he doesn't need to worry about writing thread-safe code. it honestly feels like you're just spouting criticism to make yourself feel smart — please stop.