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
29 changes: 29 additions & 0 deletions src/main/java/org/fundacionjala/coding/jose/FrequentItem.java
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++) {
Copy link
Contributor

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.

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 src/test/java/org/fundacionjala/coding/jose/FrequentItemTest.java
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);
}
}