-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question1.js
42 lines (40 loc) · 938 Bytes
/
Question1.js
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
function answer(targetArray) {
let trackElements = [];
let finalArray = [];
let num = 0;
targetArray.sort( (a,b) => a-b );
let localArray = (element, count) => {
let a = [];
for( let i = 0; i < num; i++){
a.push(element)
} return a;
}
let isPresent = (item) => {
if(trackElements.length === 0)
return false;
for(let i = 0; i < trackElements.length; i++){
if(item === trackElements[i])
return true
} return false;
}
for (let i = 0; i < targetArray.length; i++){
if(isPresent(targetArray[i]))
continue;
else{
for (let j = 0; j < targetArray.length; j++){
if(targetArray[i] === targetArray[j])
num++;
}
if(num === 1){
trackElements.push(targetArray[i]);
finalArray.push(targetArray[i]);
num = 0;
} else {
trackElements.push(targetArray[i]);
finalArray.push(localArray(targetArray[i], num));
num = 0;
}
}
}
return finalArray;
}