# A.1.1: Arrays

- 1.Understand common array functions and their use cases
- 2.Get familiar with solving algorithm problems with arrays

Assume we start with the following example array

`arr`

. Scroll right in the table below to see explanations.const arr = [2, 1, 3];

Function | Resulting value of `arr` | Return value | Explanation |
---|---|---|---|

`arr[1]` | `[2,1,3]` | `1` | We can access value at specific index in array in a single operation |

`arr.push(4)` | `[2,1,3,4]` | `4` | We can append to end of array in single operation |

`arr.length` | `[2,1,3]` | `3` | JS Array data structure stores up-to-date length that we can retrieve in constant time |

`Math.max(...arr)` | `[2,1,3]` | `3` | Get max element of unsorted array requires iterating over every element in array |

`arr.shift()` | `[1, 3]` | `2` | Removing element from start of array requires shifting every element to the left by 1 index |

`arr.unshift(4)` | `[4,2,1,3]` | `4` | Adding element to start of array requires shifting every element to the right by 1 index |

`arr.splice(1, 0, 4)` | `[2,4,1,3]` | `[]` | Adding and removing elements from the middle of an array requires shifting every following element by a constant number of indexes |

`arr.sort()` | `[1,2,3]` | `[1,2,3]` | The fastest sorting algorithms run in `O(nlogn)` time, different JS runtimes implement different sorting algorithms that all have similar runtimes. |

After attempting each problem, find solutions in the Leaderboard tab (HackerRank, may be on left side of page) or Solution or Discuss tabs (LeetCode) on that problem's page. If you get stuck for more than 15 minutes, review and understand the solutions and move on. Come back and re-attempt the problem after a few days.

- 1.
- 2.
- 3.
- 4.
- 5.

- 1.
- 2.
- 3.
- 4.

- 1.
- 2.
- 3.
- 1.Hint: You may find the array
`sort`

method helpful to sort input array by word length - 2.Hint: You may find nested for loops helpful where the indexes follow the pattern in the below code sample

- 4.

Code sample for String Matching in an Array:

for (let i = 0; i <= arr; i += 1) {

for (let j = i+1; j <= arr; j += 1) {

// Compare arr[i] and arr[j] without duplicate checks

}

}

Last modified 2mo ago