Skip to content

Commit a40755d

Browse files
authored
Merge pull request #71 from rimsha-shoukat/main
Add length converter and remove duplicates problems with solutions and README
2 parents fe7ba4f + 8799e77 commit a40755d

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 0027 removeDuplicates ( L-B )
2+
3+
## Problem
4+
Write a function that removes duplicate values from the given input.
5+
- If the input is an array, return a new array with unique values.
6+
- If the input is a string, return a new string with duplicate characters removed.
7+
- The original order must be preserved.
8+
9+
## Solution
10+
```js
11+
function removeDuplicates(input) {
12+
let unique = [];
13+
for (let i = 0; i < input.length; i++) {
14+
if (!unique.includes(input[i])) {
15+
unique.push(input[i]);
16+
}
17+
}
18+
if(typeof(input) === "string"){
19+
unique = unique.toString().replace(/,/g, '');
20+
}
21+
console.log(unique);
22+
return unique;
23+
}
24+
```
25+
26+
## Test Cases
27+
```js
28+
removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]);
29+
// Output: [2, 4, 8, 6, 9, 10]
30+
31+
removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]);
32+
// Output: [1, 4, 0, 6, -2, 2, 7, 10]
33+
34+
removeDuplicates("zoom");
35+
// Output: "zom"
36+
37+
removeDuplicates("hello world");
38+
// Output: "helo wrd"
39+
```
40+
41+
## How it works
42+
- Create an empty array called unique
43+
- Loop through each character or element in the input
44+
- Check if the current value already exists in unique
45+
- If it does not exist, we add it to unique
46+
- After the loop:
47+
- If the input is a string, we convert the array into a string
48+
- Remove commas using replace
49+
- Finally, return the result
50+
51+
## References
52+
- [MDN – Array.prototype.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)
53+
- [MDN – typeof operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
54+
55+
56+
## Problem Added By
57+
- [GitHub](https://github.com/rimsha-shoukat)
58+
59+
## Contributing
60+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
61+
62+
Please make sure to update tests as appropriate.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function removeDuplicates(input) {
2+
let unique = [];
3+
for (let i = 0; i < input.length; i++) {
4+
if (!unique.includes(input[i])) {
5+
unique.push(input[i]);
6+
}
7+
}
8+
if(typeof(input) === "string"){
9+
unique = unique.toString().replace(/,/g, '');
10+
}
11+
console.log(unique);
12+
return unique;
13+
}
14+
15+
removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]);
16+
removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]);
17+
removeDuplicates("zoom");
18+
removeDuplicates("hello world");
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 0012 lengthConverter ( L-I )
2+
3+
## Problem
4+
Create a function that converts a given length value from one unit to another.
5+
The function should:
6+
- Accept a numeric length
7+
- Accept a fromUnit and toUnit
8+
- Support common metric and imperial units
9+
- Return the converted value rounded to 5 decimal places
10+
- Throw an error if an invalid unit is provided
11+
12+
## Supported Units
13+
- meters
14+
- kilometers
15+
- centimeters
16+
- millimeters
17+
- inches
18+
- feet
19+
- yards
20+
- miles
21+
22+
## Solution
23+
```js
24+
function lengthConverter(length, fromUnit, toUnit) {
25+
const units = {
26+
"meters": 1,
27+
"kilometers": 1000,
28+
"centimeters": 0.01,
29+
"millimeters": 0.001,
30+
"inches": 0.0254,
31+
"feet": 0.3048,
32+
"yards": 0.9144,
33+
"miles": 1609.34
34+
};
35+
36+
if (!units[fromUnit] || !units[toUnit]) {
37+
throw new Error("Invalid unit");
38+
}
39+
const meters = length * units[fromUnit];
40+
console.log((meters / units[toUnit]).toFixed(3));
41+
return (meters / units[toUnit]).toFixed(3);
42+
}
43+
```
44+
45+
## Test Cases
46+
```js
47+
removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]);
48+
// Output: [2, 4, 8, 6, 9, 10]
49+
50+
removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]);
51+
// Output: [1, 4, 0, 6, -2, 2, 7, 10]
52+
53+
removeDuplicates("zoom");
54+
// Output: "zom"
55+
56+
removeDuplicates("hello world");
57+
// Output: "helo wrd"
58+
```
59+
60+
## How it works
61+
- Define a units object where each unit is mapped to its value in meters
62+
- Validate both fromUnit and toUnit
63+
- The input length is first converted into meters
64+
- Then we convert meters into the target unit
65+
- The result is rounded to 5 decimal places using toFixed(5)
66+
- Finally, the converted value is returned
67+
68+
## References
69+
- [Wikipedia – Unit conversion](https://en.wikipedia.org/wiki/Unit_conversion)
70+
- [MDN – JavaScript Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects)
71+
72+
73+
## Problem Added By
74+
- [GitHub](https://github.com/rimsha-shoukat)
75+
76+
## Contributing
77+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
78+
79+
Please make sure to update tests as appropriate.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function lengthConverter(length, fromUnit, toUnit) {
2+
const units = {
3+
"meters": 1,
4+
"kilometers": 1000,
5+
"centimeters": 0.01,
6+
"millimeters": 0.001,
7+
"inches": 0.0254,
8+
"feet": 0.3048,
9+
"yards": 0.9144,
10+
"miles": 1609.34
11+
};
12+
13+
if (!units[fromUnit] || !units[toUnit]) {
14+
throw new Error("Invalid unit");
15+
}
16+
17+
const meters = length * units[fromUnit];
18+
console.log((meters / units[toUnit]).toFixed(2));
19+
return (meters / units[toUnit]).toFixed(2);
20+
}
21+
22+
lengthConverter(100, "meters", "kilometers");
23+
lengthConverter(5, "miles", "feet");
24+
lengthConverter(12, "inches", "centimeters");
25+
lengthConverter(2500, "centimeters", "meters");
26+
lengthConverter(3, "kilometers", "miles");
27+
lengthConverter(10, "feet", "yards");
28+
lengthConverter(5000, "millimeters", "meters");
29+
lengthConverter(2, "yards", "inches");

0 commit comments

Comments
 (0)