|
6 | 6 | 1. [Important Methods](#methods)
|
7 | 7 |
|
8 | 8 | ## Methods
|
9 | | -> Most important javascript build in methods |
| 9 | +> Hello Javascript |
10 | 10 |
|
11 | 11 | <a name="typeof"></a><a name="1.1"></a>
|
12 | 12 | - [1.1](#typeof) **typeof**: Returns the type.
|
@@ -118,8 +118,30 @@ console.log(user); // TypeError: Cannot assign to read only property 'age' of ob
|
118 | 118 | ```
|
119 | 119 |
|
120 | 120 | <a name="rename"></a><a name="1.7"></a>
|
121 | | -- [1.5](#rename) **rename**: Rename multiple files extentions at once by a command (Just for Win). |
| 121 | +- [1.7](#rename) **rename**: Rename multiple files extentions at once by a command (Just for Win). |
122 | 122 |
|
123 | 123 | ```javascript
|
124 | 124 | Get-ChildItem *.css | Rename-Item -NewName { $_.name -Replace '\.css','.scss' }
|
125 | 125 | ```
|
| 126 | + |
| 127 | + <a name="majority"></a><a name="1.8"></a> |
| 128 | +- [1.8](#majority) **majority**: Find Majority Element. |
| 129 | + |
| 130 | +```javascript |
| 131 | +function majorityElement(arr) { |
| 132 | + let count = 0, candidate = null; |
| 133 | + |
| 134 | + for (let num of arr) { |
| 135 | + if (count === 0) candidate = num; |
| 136 | + count += (num === candidate) ? 1 : -1; |
| 137 | + } |
| 138 | + |
| 139 | + return candidate; |
| 140 | +} |
| 141 | + |
| 142 | +// Time complexity: O(n) |
| 143 | +// Space complexity: O(1) |
| 144 | + |
| 145 | +const arr = [3, 2, 3, 4, 3, 1, 6, 6, 7, 8, 6, 9, 6]; |
| 146 | +console.log(majorityElement(arr)); // Output: 6 |
| 147 | +``` |
0 commit comments