-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducestring.js
More file actions
119 lines (99 loc) · 2.57 KB
/
Copy pathreducestring.js
File metadata and controls
119 lines (99 loc) · 2.57 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Get duplicated elements in an array
* @params array arr
*
* @returns array of duplicated elements.
*/
function duplicateds(arr) {
var idxTable = {};
var result = [];
if (!arr || arr.length < 1) return null;
// Populate index table.
arr.forEach(el => {
// If index does not exist create it.
if (!idxTable[el]) {
idxTable[el] = 0;
}
// For each element add 1 to the index table.
idxTable[el] += 1;
});
// For each arr duplicated element save this index to result.
for (let prop in idxTable) {
// If duplicated prop
if (idxTable[prop] > 1) {
// Push index to result;
result.push(prop);
}
}
// Return result
return result;
}
/**
* Get all occurrences of a element in an array
* @params array arr
* @params any find
*
* @returns array indexes of a given element.
*/
function getAllOccurrences(arr, find) {
if (!arr || !arr.length) return null;
// a = accumulated initial [].
// e = element.
// i = index.
return arr.reduce((a, e, i) => {
// if element equals find push index to accumulated.
if (e === find) {
a.push(i);
}
// Return accumulated.
return a;
}, []);
}
/**
* Remove duplicated items from an array.
*
* @param array arr
*
* @returns array
*/
function removeDuplicates(arr) {
if (!arr || !arr.length) return null;
return arr.filter((item, position, self) => self.indexOf(item) === position);
}
function mostre(str) {
if (!str || !str.length) return null;
const letters = str.match(/(\D)+/g);
const numbers = str.match(/(\d)+/g);
// console.log('letters :', letters);
// console.log('numbers :', numbers);
const find = duplicateds(letters);
// console.log('find :', find);
const els = find.map(el => getAllOccurrences(letters, el));
// console.log('els :', els);
const numbersCopy = numbers.slice();
// console.log('numbers :', numbers);
let sum = els.map(el =>
el.map(e => numbers[e]).reduce((acu, cur) => +acu + +cur)
);
// console.log('sum :', sum);
// All not duplicated.
els.forEach(el => {
el.forEach(elm => {
const i = numbers.indexOf(numbersCopy[elm]);
numbers.splice(i, 1);
});
});
// Add the sum to the first position of duplicated el.
els.forEach((el, i) => {
numbers.splice(els[i][0], 0, sum[i]);
});
// console.log('numbers :', numbers);
const lettersSingle = removeDuplicates(letters);
const final = lettersSingle
.map((el, i) => [el, numbers[i]])
.map(el => el.join(''))
.join('');
console.log('final :', final);
}
mostre('a12c56a1b5');
mostre('a12c56c2a1b5j654h77');