-
Notifications
You must be signed in to change notification settings - Fork 2
Resolving kata most frequent item in a collection. #145
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
Open
JoseTorrez
wants to merge
1
commit into
develop
Choose a base branch
from
josetorrez_most_frequent_item_in_array
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/org/fundacionjala/coding/jose/FrequentItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package org.fundacionjala.coding.jose; | ||
|
|
||
| /** | ||
| * Created by JoseTorrez on 9/5/2017. | ||
| */ | ||
| public class FrequentItem { | ||
|
|
||
| /** | ||
| * This method is for find the most frequent number in a collection. | ||
| * @param collection of numbers received. | ||
| * @return the item most repeated in the collection. | ||
| */ | ||
| public int mostFrequentItemCount(int[] collection) { | ||
| int most = 0; | ||
| for (int i = 0; i < collection.length; i++) { | ||
| int count = 0; | ||
| for (int x : collection) { | ||
| if (x == collection[i]) { | ||
| count++; | ||
| } | ||
| } | ||
| if (count > most) { | ||
| most = count; | ||
| } | ||
| } | ||
| return most; | ||
| } | ||
|
|
||
| } | ||
89 changes: 89 additions & 0 deletions
89
src/test/java/org/fundacionjala/coding/jose/FrequentItemTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package org.fundacionjala.coding.jose; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Random; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Created by JoseTorrez on 9/5/2017. | ||
| */ | ||
| public class FrequentItemTest { | ||
| private FrequentItem frequentItem; | ||
|
|
||
| /** | ||
| * Initializator. | ||
| */ | ||
| @Before | ||
| public void setUp() { | ||
| frequentItem = new FrequentItem(); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Basic Test. | ||
| */ | ||
| @Test | ||
| public void basicTests() { | ||
| String msg = "Should work with example test cases"; | ||
| assertEquals(msg, 2, frequentItem.mostFrequentItemCount(new int[]{3, -1, -1})); | ||
| assertEquals(msg, 5, | ||
| frequentItem.mostFrequentItemCount(new int[]{3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3})); | ||
| } | ||
|
|
||
| /** | ||
| * Edge Test. | ||
| */ | ||
| @Test | ||
| public void edgeTests() { | ||
| assertEquals("Should work for empty arrays", 0, | ||
| frequentItem.mostFrequentItemCount(new int[0])); | ||
| assertEquals("Should work for 1-element arrays", 1, | ||
| frequentItem.mostFrequentItemCount(new int[]{9})); | ||
| assertEquals("Should work with multiple most frequent items, e.g. nine 7's and nine 1's", 3, | ||
| frequentItem.mostFrequentItemCount(new int[]{7, 1, 7, 1, 7, 1})); | ||
| } | ||
|
|
||
| /** | ||
| * Test for numbers generated by random. | ||
| */ | ||
| @Test | ||
| public void randomTests() { | ||
| Random randGen = new Random(); | ||
| System.out.println("Testing 40 random arrays..."); | ||
| for (int i = 0; i < 40; i++) { | ||
| int[] testArr = new int[randGen.nextInt(30)]; | ||
| for (int e = 0; e < testArr.length; e++) { | ||
| testArr[e] = randGen.nextInt(30) - 15; | ||
| } | ||
| assertEquals(mostFrequentAns(testArr), | ||
| frequentItem.mostFrequentItemCount(testArr)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param collection Integer. | ||
| * @return Integer. | ||
| */ | ||
| private int mostFrequentAns(int[] collection) { | ||
| int max = 0; | ||
| int currMax = 0; | ||
| int currNum = 0; | ||
| Arrays.sort(collection); | ||
| for (int i = 0; i < collection.length; i++) { | ||
| if (currNum != collection[i]) { | ||
| if (currMax > max) { | ||
| max = currMax; | ||
| } | ||
| currMax = 1; | ||
| currNum = collection[i]; | ||
| } else { | ||
| currMax++; | ||
| } | ||
| } | ||
| return (currMax > max ? currMax : max); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a for each instead of a for loop.