1

I working with a really large array and I'm removing few elements from it. By now, every time I want to remove an element without knowing its index, I'm actually doing something like:

const newarr = arr.filter(x => x !== y) 

So, everytime I need to remove an element I'm running in O(n) complexity. I think worth creating an structure where I can remove an element from array in O(1) later.

For design purposes, it is an array of references and there aren't repeated elements.

I know I must somehow create an object with index as values and some convenient string as keys, so I can access index in O(1) and remove it using:

const newarr = [...arr.slice(0, i), ...arr.slice(i + 1, arr.length - 1)]

So, how to create this object? Is this a good approach?

17
  • 1
    It is not possible to remove an element from an array in O(1) time. Commented Aug 9, 2019 at 12:39
  • 1
    I'm voting to close this question as off-topic because the request is impossible to satisfy. Commented Aug 9, 2019 at 12:40
  • 3
    "In C, I could create this structure using point[er]s". Somehow, I doubt it. Your requirements seem inconsistent. But, perhaps I misunderstand what you are driving at. If you do have a C-implementation of a data structure, put it in your question and ask how you could get similar functionality with JS. Commented Aug 9, 2019 at 12:44
  • 1
    @guijob I'm sorry but, as long as you need to preserve the original array, I don't think you can do that in O(1), because you still would need to traverse the array if you need to filter items out. Morever, you mention that the original array shouldn't be modified: in your current approach, either slicing either filtering still keeps the "references" to the old array so, if you somehow alter the new array, the old one will be altered as well, which is something you should take care about as well. As a side note, you couldn't do that in C either, not in this way, at least... Commented Aug 9, 2019 at 12:49
  • 1
    Does it has to be an array? Or could it be something else? Commented Aug 9, 2019 at 12:52

3 Answers 3

3

Let's see time complexities of some operations of array:

  1. Adding item is O(1). As insertion does not shift the index. It only adds a new index for the new item at the end of the array
  2. Remove is O(N) because array is shifted
  3. Remove by index is O(N) as it is necessary to shift elements at higher indices
  4. Remove an element at the end is O(1) because there is no shifting.

So you need to use another type of data structure to have O(1) of deletion.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you. certainly an array isn't a good data structure for this kind of problem.
Man, two questions: 1) Are you sure about adding item being O(n)? I'm asking because this doesn't seems so logical. 2) Does Map.delete() has O(1)?
push to the array is O(1), its just put new item at the end of the array and update its size
@FilipKováč yeah, you are right! Thanks for your great comment! I've edited my reply! :)
0

A bit late to the party, but I just came here from Google, so whoever else finds this thread, the most consize implementation of the O(1) queue that I have found so far is here. I just changed a couple of typos and switched to Map to maintain the order of properties. Live sample with tests.

Implementation

function Queue() {

  this.items = new Map();
  this.tail = 0;
  this.head = 0;

  this.enqueue = (e) => {
    this.items[this.tail++] = e;
  }

  this.dequeue = () => {
    if (this.tail === this.head) return null;
    let e = this.items[this.head];
    delete this.items[this.head++];
    return e;
  }
}

Tests

const queue = new Queue();

queue.enqueue("W");
queue.enqueue("A");
queue.enqueue("C");

console.log(queue.items)

for (let i in queue.items) {
    console.log('Iteration => ', i, ' => ', queue.items[i])
}

console.log('Expect W => ', queue.dequeue());
console.log('Expect A => ', queue.dequeue());
console.log('Expect C => ', queue.dequeue());

Comments

0

I think that everyone is probably aware, but I will write it again. In fact, deleting by index is O(1) in JS) If you don't care about the order... myarray[indexToDelete] = myarray[myarray.length - 1]; myarray.pop();

1 Comment

This doesn't address the OP's question - they don't know the index of the element to be deleted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.