2022-07-02 16:30:14 +01:00
|
|
|
---
|
2024-06-16 18:30:03 +01:00
|
|
|
tags:
|
|
|
|
- design-patterns
|
2022-07-02 16:30:14 +01:00
|
|
|
---
|
2024-02-02 15:58:13 +00:00
|
|
|
|
2022-07-02 16:30:14 +01:00
|
|
|
# Multiple pointers
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
The multiple pointers pattern can be useful for moving through an array and
|
|
|
|
comparing elements against each other in a time-efficient manner and without
|
|
|
|
costly multiple loops.
|
2022-07-02 16:30:14 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
## Example: moving pairs
|
2022-07-02 16:30:14 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
In this example we shuffle through an array creating a pair from each element
|
|
|
|
encountered. Thus for an array such as `[4, 3, 2, 1, 3, 6, 7]` we would return
|
|
|
|
`[ [ 4, 3 ], [ 3, 2 ], [ 2, 1 ], [ 1, 3 ], [ 3, 6 ], [ 6, 7 ] ]`.
|
2022-07-02 16:30:14 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function movingPairs(arr) {
|
|
|
|
const store = [];
|
|
|
|
let left = 0;
|
|
|
|
let right = 1;
|
|
|
|
for (left; left < arr.length - right; left++) {
|
|
|
|
store.push([arr[left], arr[right + left]]);
|
|
|
|
}
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
```
|
2024-02-02 15:58:13 +00:00
|
|
|
|
2022-07-02 16:30:14 +01:00
|
|
|
## Example: identify unique values
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
An example application of the above pattern would be identifying the number of
|
|
|
|
unique values in an array that contains duplicates. One way to do this in
|
|
|
|
JavaScript would be to use a Set however this is a more generic approach that
|
|
|
|
only requires a single loop:
|
2022-07-02 16:30:14 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function uniqueValues(arr) {
|
|
|
|
if (!arr.length) {
|
2024-02-02 15:58:13 +00:00
|
|
|
return 0;
|
2022-07-02 16:30:14 +01:00
|
|
|
} else {
|
2024-02-02 15:58:13 +00:00
|
|
|
let count = 1;
|
|
|
|
let left = 0;
|
|
|
|
let right = 1;
|
2022-07-02 16:30:14 +01:00
|
|
|
for (right; right < arr.length; right++) {
|
|
|
|
if (arr[left] !== arr[right]) {
|
2024-02-02 15:58:13 +00:00
|
|
|
count++;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
2024-02-02 15:58:13 +00:00
|
|
|
left++;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
return count;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2024-02-02 15:58:13 +00:00
|
|
|
|
|
|
|
This works by having two pointers at either end of the array; one moving from
|
|
|
|
the right and one moving from the left. Whilst the right pointer is less than
|
|
|
|
the array length, the left pointer is incremented as the right moves closer to
|
|
|
|
it. At each incrementation, the values of pointers (used as indexes) are
|
|
|
|
compared for uniqueness, and a count is kept.
|
2022-07-02 16:30:14 +01:00
|
|
|
|
|
|
|
## Example: identify duplicates
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This is very similar to the above but this time we are looking for duplicates
|
|
|
|
rather than unique values.
|
2022-07-02 16:30:14 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function areThereDuplicatesPointer(...input) {
|
2024-02-02 15:58:13 +00:00
|
|
|
let start = 0;
|
|
|
|
let next = 1;
|
2022-07-02 16:30:14 +01:00
|
|
|
while (next < input.length) {
|
2024-02-02 15:58:13 +00:00
|
|
|
let same = input[start] === input[next];
|
2022-07-02 16:30:14 +01:00
|
|
|
if (same) {
|
2024-02-02 15:58:13 +00:00
|
|
|
return true;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
2024-02-02 15:58:13 +00:00
|
|
|
start++;
|
|
|
|
next++;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
return false;
|
2022-07-02 16:30:14 +01:00
|
|
|
}
|
2024-02-02 15:58:13 +00:00
|
|
|
console.log(areThereDuplicatesPointer(1, 2, 3)); // false
|
|
|
|
console.log(areThereDuplicatesPointer(1, 2, 2)); // true
|
|
|
|
```
|