From 6843940e685b29167f740e1808ecb13ed8144d2e Mon Sep 17 00:00:00 2001 From: Gioserden Date: Wed, 30 Aug 2017 19:45:27 +0000 Subject: [PATCH 1/2] Kata Most frequent number --- .../coding/sergio/MostFrequent.java | 37 +++++++++++++++++++ .../coding/sergio/MostFrequentTest.java | 20 ++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java create mode 100644 src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java diff --git a/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java b/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java new file mode 100644 index 0000000..e65c0fe --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java @@ -0,0 +1,37 @@ +package org.fundacionjala.coding.sergio; + +/** + * + */ +public final class MostFrequent { + + /*** + * + */ + private MostFrequent() { + + } + + /** + * @param collection of numbers. + * @return number. + */ + static int mostFrequentItemCount(int[] collection) { + int count = 1, tempCount = 0; + int temp = 0; + for (int i = 0; i < (collection.length - 1); i++) { + temp = collection[i]; + tempCount = 0; + for (int j = 1; j < collection.length; j++) { + if (temp == collection[j]) { + tempCount++; + } + if (tempCount > count) { + count = tempCount; + } + } + + } + return count; + } +} diff --git a/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java b/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java new file mode 100644 index 0000000..6d11cbd --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java @@ -0,0 +1,20 @@ +package org.fundacionjala.coding.sergio; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class MostFrequentTest { + + /** + * + */ + @Test + public void tests() { + assertEquals(2, MostFrequent.mostFrequentItemCount(new int[]{3, -1, -1})); + assertEquals(5, MostFrequent.mostFrequentItemCount(new int[]{3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3})); + } +} From 615d75de8f180b23f93d17357d0b5bf2df4d9d2c Mon Sep 17 00:00:00 2001 From: Gioserden Date: Wed, 30 Aug 2017 21:48:41 +0000 Subject: [PATCH 2/2] Adding constructor test --- .../coding/sergio/MostFrequent.java | 2 +- .../coding/sergio/MostFrequentTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java b/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java index e65c0fe..1e0ba9e 100644 --- a/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java +++ b/src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java @@ -16,7 +16,7 @@ private MostFrequent() { * @param collection of numbers. * @return number. */ - static int mostFrequentItemCount(int[] collection) { + static int mostFrequentItemCount(int[] collection) { int count = 1, tempCount = 0; int temp = 0; for (int i = 0; i < (collection.length - 1); i++) { diff --git a/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java b/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java index 6d11cbd..f88f705 100644 --- a/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java +++ b/src/test/java/org/fundacionjala/coding/sergio/MostFrequentTest.java @@ -2,13 +2,35 @@ import org.junit.Test; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * */ public class MostFrequentTest { + /** + * This test assert the modifier of the Sequence private constructor class. + * + * @throws NoSuchMethodException throw when no private constructor is defined. + * @throws IllegalAccessException throw when can not access to the constructor. + * @throws InvocationTargetException throw when can not be perform a invocation. + * @throws InstantiationException throw when a instantiation can not be performed. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = MostFrequent.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + /** * */