-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge-3.js
66 lines (54 loc) · 2.14 KB
/
challenge-3.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Take a list of integers and find the common digit?
// Create a file using any language(javascript preferred but not required)
// and name it challenge-3.
// Array of number [11232, 1712, 2311] common digits = [1, 1, 2]
// (if a digit occurs x times in all numbers,
// you need to include that digit x times in your answer)
const findCommonDigits = arr => {
let commDigits = [];
let intCount = {}
// create a set which returns unique keys only.
let allInts = new Set();
arr.forEach(ele => {
ele.toString().split("").forEach(int => {
allInts.add(int)
})
})
// iterate over our original array and the set which will modify our intCount dictionairy. This Dictionairy will store the set's key
// to the occurence at the current iteration. If the occurrence is less than the previous than it will replace the previous occurence count.
// This is to ensure that each integer has a shared occurence for the common digits. IntCount will look like this for the given example above
// { '1': 2, '2': 1, '3': 0, '7': 0 }
arr.forEach(ele1 => {
allInts.forEach(ele2 => {
if (ele2 in intCount) {
let newVal = Math.min(intCount[ele2], returnOccurence(ele1, ele2))
intCount[ele2] = newVal
} else {
intCount[ele2] = returnOccurence(ele1, ele2)
}
})
})
// iterate over our keys while keying into the the dictionairy formed above.
// Then we push into an array while the count of occurence is greater than 0.
Object.keys(intCount).forEach(num => {
while (intCount[num] > 0) {
commDigits.push(num)
intCount[num] -= 1
}
})
// return the array
return commDigits
}
// helper function that returns the occurence of a integer within a given larger number
const returnOccurence = (wholeNum, num) => {
wholeNum = wholeNum.toString();
num = num.toString();
counter = 0;
wholeNum.split("").forEach(ele => {
if (num === ele) {
counter += 1;
}
})
return counter;
}
findCommonDigits([11232, 1712, 2311])