An array is collection of items stored at contiguous memory locations. The idea is to store multiple items of same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value
- Arrays allow random access of elements. This makes accessing elements by position faster
- You can read or write a list item by referring to its index in constant time
- Arrays have better cache locality that can make a pretty big difference in performance
- Excessive read, as time complexity of read is always O(1)
- Random access to element using index
- Access: O(1)
- Search: O(n)
- Insertion: O(n)
- Deletion: O(n)
- add(int i, E element): O(n - i)
- remove(int i): O(n - i)
- removeRange(int i, int j): O(n - i)
- remove(Object o): O(n)
- contains(Object o): O(n)
- indexOf(Object o): O(n)
- lastIndexOf(Object o): O(n)
Methods | Description | Returns | Notes |
---|---|---|---|
asList | This method returns a fixed-size list backed by the specified array | List | |
binarySearch | Searches the specified array for the specified value using the binary search algorithm | int | -1 if not found otherwise 0 or more index number |
copyOf | Copies the specified array, truncating or padding with defaults (if necessary) so the copy has the specified length | [] | |
copyOfRange | Copies the specified range of the specified array into a new array | [] | |
deepEquals | Returns true if the two specified arrays are deeply equal to one another | Boolean | |
equals | Returns true if the two specified arrays of objects are equal to one another | Boolean | |
fill | Assigns the specified value to each element of the specified range of the specified array | void | |
sort | Sorts the specified array into ascending numerical order | void |
- Initializing Arrays in java
int[] i = new int[4];
int[] i = new int[]{1,2,3,4};
- Copy Arrays in java
Arrays.copyOf(originalArray, lengthOfArray);
Arrays.copyOfRange(originalArray, fromIndex, toIndex);
Arrays.asList(/* pass objects as list */);
Remove Duplicates from Sorted Array:
Solution (Easy)Best Time to Buy and Sell Stock II:
Solution (Easy)Rotate Array
Solution (Easy)Contains Duplicate
Solution (Easy)Single Number
Solution (Easy)Intersection of 2 Arrays
Solution (Easy)Plus One
Solution (Easy)Move Zeros
Solution (Easy)Two Sum
Solution (Easy)Valid Sudoku
Solution (Easy)Rotate Image
Solution (Medium)3 Sum
Solution (Medium)Set Matrix Zeros
Solution (Medium)Group Anagrams
Solution (Medium)Increasing Triplet Subsequence
Solution (Medium)Merge Two Sorted Arrays
Solution (Easy)First Bad Version
Solution (Easy)FizzBuzz
Solution (Easy)Count Prime
Solution (Easy)Is Power of Three
Solution (Easy)Pascal's Triangle
Solution (Easy)Missing Number
Solution (Easy)Search Insert Position
Solution (Easy)Sort Colors
Solution (Medium)Top K Frequent Elements
Solution (Medium)Kth Largest Elements
Solution (Medium)Find Peak Element
Solution (Medium)Find First and Last Position of Element in Sorted Array
Solution with O(n) (Medium) - PendingO(log n)
solutionMerge Intervals
Solution (Medium)Non-overlapping Intervals
Solution (Medium) (Greedy Algorithm - Need to understand that)