-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2.js
More file actions
21 lines (17 loc) · 643 Bytes
/
Copy pathp2.js
File metadata and controls
21 lines (17 loc) · 643 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Problem-2: Remove Duplicate Numbers
const nums = [2, 5, 5, 8, 10, 10, 15];
// A reusable function
const removeDuplicates = (arr) => {
let newArr = []; // An empty array to store numbers
// A loop to check the numbers of the array, one by one
for (let i = 0; i < arr.length; i++) {
// Check the the number from the array, based on index
// If the number does not includes in the new array
if (!newArr.includes(arr[i])) {
newArr.push(arr[i]); // Push the number in the new array
}
}
return newArr; // Return the new array
};
// Let's run
console.log(removeDuplicates(nums)); // Output: [ 2, 5, 8, 10, 15 ]