Skip to content

Implementation of finding the smallest common number #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added solution for finding the smallest common number
  • Loading branch information
jdhrnndz committed Oct 6, 2019
commit 0868c0fa3103541d7802445c4fcd0146110b8ecb
29 changes: 29 additions & 0 deletions src/_Problems_/get-smallest-common-number/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Get the common smallest number between two integer arrays

const getSmallestCommonNumber = (a1, a2) => {
let map = {};
let i = 0;
let min;

while (a1.length > i || a2.length > i) {
if (i < a1.length) {
map[`${a1[i]}a`] = true;
if (map[`${a1[i]}b`] && (min > a1[i] || !min)) {
min = a1[i];
}
}

if (i < a2.length) {
map[`${a2[i]}b`] = true;
if (map[`${a2[i]}a`] && (min > a2[i] || !min)) {
min = a2[i];
}
}

i++;
}

return min || -1;
};

module.exports = { getSmallestCommonNumber };