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

Lesson 2: mostCommon - Takes in an array of numbers & find the most common number (using objects might help) #47

Open
seemcat opened this issue Oct 17, 2017 · 3 comments

Comments

@seemcat
Copy link

seemcat commented Oct 17, 2017

Input: [1,0,2,2,4]
Output: 2

Input: [0,0,0,1,1,1,1]
Output: 1

@songz
Copy link

songz commented Oct 18, 2017

const val = arr[i];
if(!map[val]) map[val] = 0;
map[val] = map[val] + 1;

Object.keys(map) // returns [1,0,2,4]

@songz songz changed the title Lesson 2: mostCommon - Takes in an array of numbers & find the most common one using objects. Lesson 2: mostCommon - Takes in an array of numbers & find the most common number using objects. Oct 18, 2017
@songz songz changed the title Lesson 2: mostCommon - Takes in an array of numbers & find the most common number using objects. Lesson 2: mostCommon - Takes in an array of numbers & find the most common number (using objects might help) Oct 18, 2017
@songz
Copy link

songz commented Oct 18, 2017

First, build an object (key is number, value is how many times that number shows up in the array)
Then, go through the object to find out the maximum value.

allopez7 pushed a commit to allopez7/algorithm that referenced this issue Oct 19, 2017
allopez7 pushed a commit to allopez7/algorithm that referenced this issue Oct 20, 2017
allopez7 pushed a commit to allopez7/algorithm that referenced this issue Oct 20, 2017
@seemcat
Copy link
Author

seemcat commented Oct 20, 2017

*Going off what Song explained; once you find the maximum value it is only clear what the most common number is.

Input: [1,0,2,2,4]
Output: 2

When you create an object for this array it will look like: { 1: 1, 0: 1, 2: 2, 4: 1 }. 2 has the maximum value because it is in the array twice. Therefore, you return 2.

allopez7 added a commit to allopez7/algorithm that referenced this issue Oct 26, 2017
Solve garageScript#47: Lesson 2: mostCommon -takes in an array of numbers & find …
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants