I'm looking for an elegant way of sorting an array by the occurrence of its elements.

For example, in:

```
['pear', 'apple', 'orange', 'apple', 'orange', 'apple']

```

the output should look like

```
['apple', 'orange', 'pear']

```

I have tried to loop through the array and save the occurrence in another temporary array, but this solution was quite bad.