Skip to content

Commit ef7418f

Browse files
committed
added remove consecutive repeated digits problem
1 parent eaf0345 commit ef7418f

File tree

1 file changed

+25
-0
lines changed
  • src/_DataStructures_/Stack/ remove-consecutive-repeated-digits

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Given an integer N, remove consecutive repeated digits from it.
3+
* Input:133445
4+
* Output:1345
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
10+
function removeConsecutiveDigits(no) {
11+
let s = new Stack();
12+
let newNo = "";
13+
//initally push first digit into stack
14+
newNo += no[0];
15+
s.push(no[0]);
16+
for (let i = 1; i < no.length; i++) {
17+
const digit = no[i];
18+
//if stack top and incoming digit is same ignore it else append to newNo.
19+
if (s.peek() !== digit) {
20+
newNo += digit;
21+
s.push(digit);
22+
}
23+
}
24+
return newNo
25+
}

0 commit comments

Comments
 (0)