Skip to content
Open
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
37 changes: 37 additions & 0 deletions src/main/java/org/fundacionjala/coding/sergio/MostFrequent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.fundacionjala.coding.sergio;

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<MostFrequent> constructor = MostFrequent.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}

/**
*
*/
@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}));
}
}