Question: Given an array `arr` and an array of indices `ind`, I'd like to rearrange `arr` **in-place** to satisfy the given indices. For example:

```
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];

rearrange(arr, ind);

console.log(arr); // => ["B", "E", "D", "F", "A", "C"]

```

Here is a possible solution that uses `O(n)` time and `O(1)` space, **but mutates `ind`**:

```
function swap(arr, i, k) {
  var temp = arr[i];
  arr[i] = arr[k];
  arr[k] = temp;
}

function rearrange(arr, ind) {
  for (var i = 0, len = arr.length; i < len; i++) {
    if (ind[i] !== i) {
      swap(arr, i, ind[i]);
      swap(ind, i, ind[i]);
    }
  }
}

```

What would be the optimal solution **if we are limited to `O(1)` space and mutating `ind` is not allowed?**

---

**Edit:** The algorithm above is wrong. See [this question](https://stackoverflow.com/q/37724874/247243).

Answer: 
```
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];

function rearrange(arr, ind){
  var map = [];
  for (var i = 0; i < arr.length; i++)   map[ind[i]] = arr[i];
  for (var i = 0; i < arr.length; i++)   arr[i] = map[i];
}

rearrange(arr, ind);

console.log(arr);

```

That works but, because I'm not a smart developper, I assume it's probably not the fastest algorithm.
I am using ind as indexes in it's own order

```
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
var obj = {}
for(var i=0;i<arr.length;i++)
    obj[ind[i]]=arr[i];
console.log(obj);

```

**[Working Fiddle](https://jsfiddle.net/cfLd4w03/)**