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 frequency of most frequent element inside an array
public static int numberOfMatches(int[] numbers) {
int mostMatches = 0;
for(int i = 0; i < numbers.length; i++) {
int matches = 0;
int holder = numbers[i];
for(int number : numbers) {
if(number == holder) {
matches++;
}
}
if(matches > mostMatches)
mostMatches = matches;
}
return mostMatches;
}
This accepts an array. It checks the length of the array, and loops for each slot.
For each slot, I create 2 variables. holder
grabs the value in the slot, and we increase matches
any time there is a match.
Once all possible matches have been found for that slot, it compares the amount of matches found to the highest amount of matches found so far.
Most frequent number in array in Java
One of the solution is to create the Map with Integer as a key and Integer as the value. You will have to iterate through the array and increase the amount of the numbers in a map.
Integer[] arr = {4, 1, 4, 1, 3};
Map<Integer,Integer> integersCount = new HashMap<Integer,Integer>();
for (Integer i : arr){
if (!integersCount.containsKey(i))
integersCount.put(i, 1);
else
integersCount.put(i, integersCount.get(i) + 1);
}
After that you can go through the map and remember the position of the element which has the greatest value.
For another solution I recommend counting the numbers in another array - read about "counting sort".
Determine the most common occurrence in an array
Using a Map<Integer, Integer>
should be simple as:
int mostFrequent(int... ary) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int a : ary) {
Integer freq = m.get(a);
m.put(a, (freq == null) ? 1 : freq + 1);
}
int max = -1;
int mostFrequent = -1;
for (Map.Entry<Integer, Integer> e : m.entrySet()) {
if (e.getValue() > max) {
mostFrequent = e.getKey();
max = e.getValue();
}
}
return mostFrequent;
}
find largest most frequent element
Step1: QuickSort on ArrayList<Integer> arr
;
Step2: Iteration on ArrayList<Integer> arr
as you have done.
Related Topics
How to Prevent Eclipse from Hanging on Startup
How to Send Java.Util.Logging to Log4J
Why Are Class Static Methods Inherited But Not Interface Static Methods
How to Enable Anti Aliasing in Arbitrary Java Apps
Why Are Java 8 Lambdas Invoked Using Invokedynamic
Comparing Boxed Long Values 127 and 128
Java 8 Stream with Batch Processing
Why Is System.Arraycopy Native in Java
Is -Djava.Library.Path=... Equivalent to System.Setproperty("Java.Library.Path", ...)
How to Obtain the Start Time and End Time of a Day
What Is the Largest Possible Heap Size with a 64-Bit Jvm
Difference Between an Application Server and a Servlet Container
Tomcat7 Starts Too Late on Ubuntu 14.04 X64 [Digitalocean]
Httpget with Https:Sslpeerunverifiedexception
Pass Array to Oracle Procedure