Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0868c0f

Browse files
committed
added solution for finding the smallest common number
1 parent 6e6c321 commit 0868c0f

File tree

1 file changed

+29
-0
lines changed
  • src/_Problems_/get-smallest-common-number

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Get the common smallest number between two integer arrays
2+
3+
const getSmallestCommonNumber = (a1, a2) => {
4+
let map = {};
5+
let i = 0;
6+
let min;
7+
8+
while (a1.length > i || a2.length > i) {
9+
if (i < a1.length) {
10+
map[`${a1[i]}a`] = true;
11+
if (map[`${a1[i]}b`] && (min > a1[i] || !min)) {
12+
min = a1[i];
13+
}
14+
}
15+
16+
if (i < a2.length) {
17+
map[`${a2[i]}b`] = true;
18+
if (map[`${a2[i]}a`] && (min > a2[i] || !min)) {
19+
min = a2[i];
20+
}
21+
}
22+
23+
i++;
24+
}
25+
26+
return min || -1;
27+
};
28+
29+
module.exports = { getSmallestCommonNumber };

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /