Skip to content
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

Implementation of finding the smallest common number #37

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list)
- [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list)
- [Stack](src/_DataStructures_/Stack)

- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
Expand All @@ -40,6 +41,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Get Maze Path](src/_Problems_/get_subsequence)
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
- [Get Max Char](src/_Problems_/maxchar)
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)
- [Merge 2 Sorted Arrays](src/_Problems_/merge-two-sorted-arrays)
- [Palindrome](src/_Problems_/palindrome)
- [Product of Elements](src/_Problems_/product-of-elements)
Expand All @@ -65,9 +67,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct

## CONTRIBUTION Guide

It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.

- When adding a new **problem** with solution

- Take care of the filename convention (Very Important)
- Problem statement should be there with examples
- Make sure you add the Run Time complexity of your solution
Expand All @@ -76,7 +79,8 @@ It's great to know that you want to contribute to this repo. Thanks for taking i
- Strictly follow ESLINT rules

- When adding a Unit Test

- Take care of the file name convention
- Make sure CI (Travis) is passing
Keep an eye on this guide, it's subjected to change frequently.

Keep an eye on this guide, it's subjected to change frequently.
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 };