-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathSegregate_0s_and_1s_in_an_arrayTS.ts
54 lines (43 loc) · 1.27 KB
/
Segregate_0s_and_1s_in_an_arrayTS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Maintain two indexes. Initialize first index left as 0 and second index right as n-1.
Do following while left < right
a) Keep incrementing index left while there are 0s at it
b) Keep decrementing index right while there are 1s at it
c) If left < right then exchange arr[left] and arr[right]
*/
// class Segregate_0s_and_1s_in_an_array {
// segregate(): void {
// var a = [0, 1, 0, 1, 1, 1];
// var first = 0;
// var last = a.length - 1;
// while (first < last) {
// while (a[first] != 1 && first < last) first++;
// while (a[last] != 0 && first < last) last--;
// if (first < last) {
// a[first] = 0;
// a[last] = 1;
// first++;
// last--;
// }
// }
// console.log(a);
// }
// }
// let Seg = new Segregate_0s_and_1s_in_an_array();
// Seg.segregate();
//Another easy TS Solution
seg([0, 1, 0, 1, 1, 1]);
function seg(arr: number[]): void {
let charMap = buildCharMap(arr);
console.log(charMap);
let zero = Array(charMap[0]).fill(0);
let one = Array(charMap[1]).fill(1);
console.log([...zero, ...one]); // [ 0, 0, 1, 1, 1, 1 ]
}
function buildCharMap(arr: number[]) {
let obj = {};
for (const item of arr) {
obj[item] = obj[item] +1 ||1;
}
return obj
}