Skip to content

Commit bd28845

Browse files
add md articles (#5001)
* add maximum-distance-in-arrays article * fix article * fix csharp method signature * fix wiggle-sort python heap solution * add confusing-number.md article * Update explanation of L in complexity analysis Clarify the definition of L in terms of n. * Fix logarithm notation in complexity section Updated logarithm notation in complexity explanation. * Fix LaTeX formatting for logarithm in documentation * add perform-string-shifts article * add one-edit-distance article * fix one-edit-dsitance article * add reverse-words-in-a-string-ii.md article * fix typo maximu-distance-in-arrays.md * fix typo reverse-words-in-a-string-ii.md * add shortest-way-to-form-string.md article * add longest-substring-with-at-most-k-distinct-characters.md article * add max-consecutive-ones-ii.md article * add find-k-length-substrings-with-no-repeated-characters.md article * add find-anagram-mappings.md article * fix latex styling find-anagram-mappings.md * add palindrome-permutation.md article * add sentence-similarity.md article * add single-row-keyboard.md article * add group-shifted-strings.md article * add largest-unique-number.md article * add counting-elements.md article * add find-smallest-common-element-in-all-rows.md article * add valid-word-square.md article * add lonely-pixel-i.md article * add sparse-matrix-multiplication.md article --------- Co-authored-by: neetcode-gh <77742485+neetcode-gh@users.noreply.github.com>
1 parent addb959 commit bd28845

File tree

7 files changed

+2708
-0
lines changed

7 files changed

+2708
-0
lines changed

articles/counting-elements.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
## 1. Search with Array
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def countElements(self, arr: List[int]) -> int:
8+
count = 0
9+
for x in arr:
10+
if x + 1 in arr:
11+
count += 1
12+
return count
13+
```
14+
15+
```java
16+
class Solution {
17+
public int countElements(int[] arr) {
18+
int count = 0;
19+
for (int x : arr) {
20+
if (integerInArray(arr, x + 1)) {
21+
count++;
22+
}
23+
}
24+
return count;
25+
}
26+
27+
public boolean integerInArray(int[] arr, int target) {
28+
for (int x : arr) {
29+
if (x == target) {
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
}
36+
```
37+
38+
```cpp
39+
class Solution {
40+
public:
41+
int countElements(vector<int>& arr) {
42+
int count = 0;
43+
for (auto x : arr) {
44+
if (integerInArray(arr, x + 1)) {
45+
count++;
46+
}
47+
}
48+
return count;
49+
}
50+
51+
bool integerInArray(vector<int>& arr, int target) {
52+
for (auto x : arr) {
53+
if (x == target) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
};
60+
```
61+
62+
```javascript
63+
class Solution {
64+
/**
65+
* @param {number[]} arr
66+
* @return {number}
67+
*/
68+
countElements(arr) {
69+
let count = 0;
70+
for (const x of arr) {
71+
if (arr.includes(x + 1)) {
72+
count++;
73+
}
74+
}
75+
return count;
76+
}
77+
}
78+
```
79+
80+
::tabs-end
81+
82+
### Time & Space Complexity
83+
84+
- Time complexity: $O(N^2)$
85+
- Space complexity: $O(1)$ constant space
86+
87+
> Where $N$ is the length of the input array `arr`.
88+
89+
---
90+
91+
## 2. Search with HashSet
92+
93+
::tabs-start
94+
95+
```python
96+
class Solution:
97+
def countElements(self, arr: List[int]) -> int:
98+
hash_set = set(arr)
99+
count = 0
100+
for x in arr:
101+
if x + 1 in hash_set:
102+
count += 1
103+
return count
104+
```
105+
106+
```java
107+
class Solution {
108+
public int countElements(int[] arr) {
109+
Set<Integer> hashSet = new HashSet<>();
110+
for (int x : arr) {
111+
hashSet.add(x);
112+
}
113+
int count = 0;
114+
for (int x : arr) {
115+
if (hashSet.contains(x + 1)) {
116+
count++;
117+
}
118+
}
119+
return count;
120+
}
121+
}
122+
```
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int countElements(vector<int>& arr) {
128+
unordered_set<int> hashSet(arr.begin(), arr.end());
129+
int count = 0;
130+
for (int x : arr) {
131+
if (hashSet.find(x + 1) != hashSet.end()) {
132+
count++;
133+
}
134+
}
135+
return count;
136+
}
137+
};
138+
```
139+
140+
```javascript
141+
class Solution {
142+
/**
143+
* @param {number[]} arr
144+
* @return {number}
145+
*/
146+
countElements(arr) {
147+
const hashSet = new Set(arr);
148+
let count = 0;
149+
for (const x of arr) {
150+
if (hashSet.has(x + 1)) {
151+
count++;
152+
}
153+
}
154+
return count;
155+
}
156+
}
157+
```
158+
159+
::tabs-end
160+
161+
### Time & Space Complexity
162+
163+
- Time complexity: $O(N)$
164+
- Space complexity: $O(N)$
165+
166+
> Where $N$ is the length of the input array `arr`.
167+
168+
---
169+
170+
## 3. Search with Sorted Array
171+
172+
::tabs-start
173+
174+
```python
175+
class Solution:
176+
def countElements(self, arr: List[int]) -> int:
177+
arr.sort()
178+
count = 0
179+
run_length = 1
180+
for i in range(1, len(arr)):
181+
if arr[i - 1] != arr[i]:
182+
if arr[i - 1] + 1 == arr[i]:
183+
count += run_length
184+
run_length = 0
185+
run_length += 1
186+
return count
187+
```
188+
189+
```java
190+
class Solution {
191+
public int countElements(int[] arr) {
192+
Arrays.sort(arr);
193+
int count = 0;
194+
int runLength = 1;
195+
for (int i = 1; i < arr.length; i++) {
196+
if (arr[i - 1] != arr[i]) {
197+
if (arr[i - 1] + 1 == arr[i]) {
198+
count += runLength;
199+
}
200+
runLength = 0;
201+
}
202+
runLength++;
203+
}
204+
return count;
205+
}
206+
}
207+
```
208+
209+
```cpp
210+
class Solution {
211+
public:
212+
int countElements(vector<int>& arr) {
213+
std::sort(arr.begin(), arr.end());
214+
int count = 0;
215+
int runLength = 1;
216+
for (int i = 1; i < arr.size(); i++) {
217+
if (arr[i - 1] != arr[i]) {
218+
if (arr[i - 1] + 1 == arr[i]) {
219+
count += runLength;
220+
}
221+
runLength = 0;
222+
}
223+
runLength++;
224+
}
225+
return count;
226+
}
227+
};
228+
```
229+
230+
```javascript
231+
class Solution {
232+
/**
233+
* @param {number[]} arr
234+
* @return {number}
235+
*/
236+
countElements(arr) {
237+
arr.sort((a, b) => a - b);
238+
let count = 0;
239+
let runLength = 1;
240+
for (let i = 1; i < arr.length; i++) {
241+
if (arr[i - 1] !== arr[i]) {
242+
if (arr[i - 1] + 1 === arr[i]) {
243+
count += runLength;
244+
}
245+
runLength = 0;
246+
}
247+
runLength++;
248+
}
249+
return count;
250+
}
251+
}
252+
```
253+
254+
::tabs-end
255+
256+
### Time & Space Complexity
257+
258+
- Time complexity: $O(N \log N)$
259+
- Space complexity: varies from $O(N)$ to $O(1)$
260+
- The overall space complexity is dependent on the space complexity of the sorting algorithm you're using. The space complexity of sorting algorithms built into programming languages are generally anywhere from $O(N)$ to $O(1)$.
261+
262+
> Where $N$ is the length of the input array `arr`.

0 commit comments

Comments
 (0)