File tree 4 files changed +79
-1
lines changed
_Problems_/remove-duplicates
4 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ describe('Data Structure : Queue', () => {
20
20
it ( 'Should remove() an element from the queue' , ( ) => {
21
21
queue . add ( 2 ) ;
22
22
queue . add ( 3 ) ;
23
- queue . remove ( ) ;
24
23
24
+ expect ( queue . remove ( ) ) . toEqual ( 2 ) ;
25
25
expect ( queue . data ) . toEqual ( [ 3 ] ) ;
26
26
} ) ;
27
27
Original file line number Diff line number Diff line change
1
+ const Stack = require ( '.' ) ;
2
+
3
+ describe ( 'Data Structure : Stack' , ( ) => {
4
+ it ( 'Should be class' , ( ) => {
5
+ expect ( typeof Stack . prototype . constructor ) . toEqual ( 'function' ) ;
6
+ } ) ;
7
+
8
+ describe ( 'Stack API' , ( ) => {
9
+ let stack = null ;
10
+
11
+ beforeEach ( ( ) => {
12
+ stack = new Stack ( ) ;
13
+ } ) ;
14
+
15
+ it ( 'Should add() element to a stack' , ( ) => {
16
+ stack . push ( 5 ) ;
17
+ expect ( stack . data ) . toEqual ( [ 5 ] ) ;
18
+ } ) ;
19
+
20
+ it ( 'Should remove() an element from the stack' , ( ) => {
21
+ stack . push ( 2 ) ;
22
+ stack . push ( 3 ) ;
23
+
24
+ expect ( stack . pop ( ) ) . toEqual ( 3 ) ;
25
+ expect ( stack . data ) . toEqual ( [ 2 ] ) ;
26
+ } ) ;
27
+
28
+ describe ( 'peek()' , ( ) => {
29
+ beforeEach ( ( ) => {
30
+ stack . push ( 2 ) ;
31
+ stack . push ( 5 ) ;
32
+ } ) ;
33
+
34
+ it ( 'Should return the elemet to be removed using peek()' , ( ) => {
35
+ expect ( stack . peek ( ) ) . toEqual ( 5 ) ;
36
+ } ) ;
37
+
38
+ it ( 'Should not remove the element' , ( ) => {
39
+ expect ( stack . peek ( ) ) . toEqual ( 5 ) ;
40
+ expect ( stack . pop ( ) ) . toEqual ( 5 ) ;
41
+ } ) ;
42
+ } ) ;
43
+
44
+ it ( 'Should maintain the FILO order of elements' , ( ) => {
45
+ // first in last out
46
+ stack . push ( 2 ) ;
47
+ stack . push ( 1 ) ;
48
+ stack . push ( 4 ) ;
49
+ stack . push ( 3 ) ;
50
+
51
+ expect ( stack . pop ( ) ) . toEqual ( 3 ) ;
52
+ expect ( stack . pop ( ) ) . toEqual ( 4 ) ;
53
+ expect ( stack . pop ( ) ) . toEqual ( 1 ) ;
54
+ expect ( stack . pop ( ) ) . toEqual ( 2 ) ;
55
+ } ) ;
56
+ } ) ;
57
+ } ) ;
Original file line number Diff line number Diff line change
1
+ class Stack {
2
+ constructor ( ) {
3
+ this . data = [ ] ;
4
+ }
5
+
6
+ push ( element ) {
7
+ // add element to the last
8
+ this . data . push ( element ) ;
9
+ }
10
+
11
+ pop ( ) {
12
+ return this . data . pop ( ) ;
13
+ }
14
+
15
+ peek ( ) {
16
+ return this . data [ this . data . length - 1 ] ;
17
+ }
18
+ }
19
+
20
+ module . exports = Stack ;
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ function removeDuplicatesUsingHashTable(str) {
9
9
}
10
10
}
11
11
12
+ // eslint-disable-next-line array-callback-return
12
13
Object . keys ( charHash ) . map ( ( char ) => {
13
14
result += char ;
14
15
} ) ;
You can’t perform that action at this time.
0 commit comments