Skip to content

Commit 9897674

Browse files
authored
Merge pull request #10 from gauravfs-14/main
[added]: Remove Duplicate from array question
2 parents bed4727 + 9d4fae3 commit 9897674

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ Write a function called `smallestCommonMultiple` that takes two numbers as its p
115115

116116
[Solution Explanation](./solutions/ch_12_Smallest_Common_Multiple/readme.md)
117117

118+
## Challenge 13: Remove Duplicates from an Array
119+
Write a function that takes an array as input and returns a new array with all duplicate elements removed. For example, if the input array is [1, 2, 2, 3, 4, 4, 5], the function should return [1, 2, 3, 4, 5].
120+
121+
Write a function called `removeDuplicates` that takes an array as its parameter and returns a new array with all duplicate elements removed.
122+
123+
[Solution Explanation](./solutions/ch_13_Remove_Duplicate_From_Array/readme.md)
124+
118125
<!-- Add new challenges before this comment -->
119126

120127
## License
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
function removeDuplicates(arr) {
3+
// Create a new array to store the unique elements
4+
let uniqueArr = [];
5+
6+
// Loop through the input array
7+
for (let i = 0; i < arr.length; i++) {
8+
// If the current element is not in the unique array, add it
9+
if (!uniqueArr.includes(arr[i])) {
10+
uniqueArr.push(arr[i]);
11+
}
12+
}
13+
14+
// Return the unique array
15+
return uniqueArr;
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Challenge 13: Remove Duplicates from an Array
2+
Write a function that takes an array as input and returns a new array with all duplicate elements removed. For example, if the input array is [1, 2, 2, 3, 4, 4, 5], the function should return [1, 2, 3, 4, 5].
3+
4+
Write a function called `removeDuplicates` that takes an array as its parameter and returns a new array with all duplicate elements removed.
5+
6+
## Answer
7+
8+
```javascript
9+
function removeDuplicates(arr) {
10+
// Create a new array to store the unique elements
11+
let uniqueArr = [];
12+
13+
// Loop through the input array
14+
for (let i = 0; i < arr.length; i++) {
15+
// If the current element is not in the unique array, add it
16+
if (!uniqueArr.includes(arr[i])) {
17+
uniqueArr.push(arr[i]);
18+
}
19+
}
20+
21+
// Return the unique array
22+
return uniqueArr;
23+
}
24+
```
25+
26+
## Answer Explanation
27+
28+
The function takes an array `arr` as input and creates a new array `uniqueArr` to store the unique elements. It then loops through the input array and checks if the current element is already in the `uniqueArr`. If it is not, the current element is added to the `uniqueArr`. Finally, the function returns the `uniqueArr`.
29+
30+
Here's an example usage of the function.
31+
32+
```javascript
33+
const arr = [1, 2, 2, 3, 4, 4, 5];
34+
const uniqueArr = removeDuplicates(arr);
35+
console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
36+
```
37+
In this example, the `removeDuplicates` function correctly removes the duplicates from the input array and returns a new array with only the unique elements.

0 commit comments

Comments
 (0)