In Java, let's say I have an integer array: int[] a = {1, 2, 3, 4, 5};
.
If I want to change an element in the array, I can do so by changing the data at a certain address in memory: a[2] = 9;
=> {1, 2, 9, 4, 5}
.
In Clojure, I can have a vector: (def a [1 2 3 4 5])
.
How do I change an element at a certain position in the vector with a guaranteed worst case time complexity of O(1)? I have read that the assoc
keyword has O(1) average time complexity, but this is not what I am looking for. Also, I looked at transient vectors, but I have yet to find a good and simple example of updating a vector in O(1).