Write a Mode Method in Java to Find the Most Frequently Occurring Element in an Array

Write a mode method in Java to find the most frequently occurring element in an array

You should use a hashmap for such problems. it will take O(n) time to enter each element into the hashmap and o(1) to retrieve the element. In the given code, I am basically taking a global max and comparing it with the value received on 'get' from the hashmap, each time I am entering an element into it, have a look:

hashmap has two parts, one is the key, the second is the value when you do a get operation on the key, its value is returned.

public static int mode(int []array)
{
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
int max = 1;
int temp = 0;

for(int i = 0; i < array.length; i++) {

if (hm.get(array[i]) != null) {

int count = hm.get(array[i]);
count++;
hm.put(array[i], count);

if(count > max) {
max = count;
temp = array[i];
}
}

else
hm.put(array[i],1);
}
return temp;
}

Finding the mode of an array method (java)

You have a bug, the two lines that you need to change are:

if(countArray[i] > mode){
mode = countArray[i];

to:

if(countArray[array[i]] > mode){
mode = array[i];

Finding the mode of an array Java

Does it have to be efficient? If not:

double maxValue = -1.0d;
int maxCount = 0;
for (int i = 0; i < data.length; ++i) {
double currentValue = data[i];
int currentCount = 1;
for (int j = i + 1; j < data.length; ++j) {
if (Math.abs(data[j] - currentValue) < epsilon) {
++currentCount;
}
}
if (currentCount > maxCount) {
maxCount = currentCount;
maxValue = currentValue;
} else if (currentCount == maxCount) {
maxValue = Double.NaN;
}
}
System.out.println("mode: " + maxValue);

Finding the Mode of Integers in an Array

Here's a simpler way to solve this problem. Create an array called count of size 101. The indexes (0-100) represent the numbers you are counting. Traverse the input array and count the occurrences of each number. Finally, compare the counts to find the one that appears the most (tie goes to the lower number):

public static int mode(int[] input) {

int[] count = new int[101];

//count the occurrences
for (int i=0; i < input.length; i++) {
count[input[i]]++;
}

//go backwards and find the count with the most occurrences
int index = count.length-1;
for (int i=count.length-2; i >=0; i--) {
if (count[i] >= count[index])
index = i;
}

return index;
}

Find the most popular element in int[] array

public int getPopularElement(int[] a)
{
int count = 1, tempCount;
int popular = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount > count)
{
popular = temp;
count = tempCount;
}
}
return popular;
}

Finding the most frequent value in an array and then choosing the lowest value if there is a tie

You could check against maxKey and then do something along the lines of:

 if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {
maxKey = counts[a[i]];
}

So that if there is ever a tie, the maxKey will be set to the smaller element. Then if count[a[i]] is ever greater than maxCount, maxKey will be overridden and become the element that occurs most often:

for(int i = 0; i < a.length; i++) {
counts[a[i]]++;

if(counts[a[i]] > maxCount) {
maxCount = counts[a[i]];
maxKey = a[i];
}
if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {
maxKey = a[i];
}
}
System.out.println(a[maxKey]);

Output

20

How to find mode of an array, if one exists. If more than one mode, return NaN

It is fairly easy to find a duplicate for this question, e.g. here.

That being said, I'd like to add another solution:

public double findMode(int[] inArray) {
List<Integer> il = IntStream.of(inArray).boxed().collect(Collectors.toList());
int maxFreq = 0;
double value = 0;
for (Integer i : ia){
if (Collections.frequency(il, i) > maxFreq && i != value){
maxFreq = Collections.frequency(il, i);
value = i;
} else if (Collections.frequency(il, i) == maxFreq && i != value) {
value = Double.NaN;
}
}
return value;
}

It turns the int[] into a List<Integer> and uses the Collections.frequency method to get the number of occurrences of each value.

Disclaimer: there are likely some optimizations that I missed.



Related Topics



Leave a reply



Submit