Skip to content

Commit e2da575

Browse files
authored
Add TypeScript solution for finding duplicates
Added TypeScript implementation for finding duplicates in an array.
1 parent 8d02634 commit e2da575

File tree

1 file changed

+21
-0
lines changed
  • solution/0400-0499/0442.Find All Duplicates in an Array

1 file changed

+21
-0
lines changed

solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ func findDuplicates(nums []int) []int {
133133
}
134134
```
135135

136+
#### TypeScript
137+
138+
```ts
139+
function findDuplicates(nums: number[]): number[] {
140+
for (let i = 0; i < nums.length; i++) {
141+
while (nums[i] !== nums[nums[i] - 1]) {
142+
const temp = nums[i];
143+
nums[i] = nums[temp - 1];
144+
nums[temp - 1] = temp;
145+
}
146+
}
147+
const ans: number[] = [];
148+
for (let i = 0; i < nums.length; i++) {
149+
if (nums[i] !== i + 1) {
150+
ans.push(nums[i]);
151+
}
152+
}
153+
return ans;
154+
}
155+
```
156+
136157
<!-- tabs:end -->
137158

138159
<!-- solution:end -->

0 commit comments

Comments
 (0)