给定一个数字数组作为输入,返回在输入中出现最大次数的数字。
MyApproach
我首先将数组中每个成员的每个数字分开。然后,我计算了每个数字的频率,然后我找到了数字出现的最大次数并记下了它的位置。当我在数字分离数组中搜索位置时,我发现了最多次出现的数字。
这是我尝试使用以下代码:
int[] seperateDigits(int[] numbers)
{
int c[]=new int[numbers.length*2];
for(int i=0;i<numbers.length;i++)
{
for(int k=0;numbers[i]>0;i++)
{
int q=numbers[i]%10; //used this logic for separation of digits
System.out.println(numbers[i]);
c[k]=q;
System.out.println(c[k]);
k++;
numbers[i]=numbers[i]/10;
}
}
return c;
}
int countMaxFreq(int c[])
{
int t[]=new int[c.length];
int count=1;
if(c.length<2)
return count;
else
{
//used the logic for finding maximum frequency of each digit.
int m=0;
for(int i=0;i<c.length;i++)
{
for(int j=i+1;j<c.length;j++)
{
if(c[j]==c[i])
{
count++;
}
}
t[m++]=count;
}
}
if(c.length<2)
return c[0];
else
{
int max=t[0];
int max_index=0;
for(int i=1;i<t.length;i++)
{
if(t[i]>=max) //used the logic for finding frequency.
{
max_index=i;
}
}
for(int l=0;l<c.length;l++)
{ //Return the position of the frequent element.
if(l==max_index)
{
break;
}
}
return max_index;
}
}
}
答案 0 :(得分:1)
您可以简化此算法的方法:
int[] counts = new int[10]
,最多10位数例如:
if (numbers.length == 0) {
throw new NoSuchElementException("no numbers, so 'most frequent' makes no sense");
}
int[] counts = new int[10];
for (int num : numbers) {
if (num == 0) {
++counts[0];
} else {
num = Math.abs(num);
while (num > 0) {
int digit = num % 10;
num /= 10;
++counts[digit];
}
}
}
return IntStream.range(0, counts.length)
.reduce((i, j) -> counts[i] < counts[j] ? j : i)
.getAsInt();
注意:当有多个数字具有相同的计数时,此实现将返回较小的数字。
答案 1 :(得分:0)
使您的函数仅返回不是其计数的变量
int countMaxFreq(int c[])
{
int t[]=new int[c.length];
int count=1;
//removed the logic here
//used the logic for finding maximum frequency of each digit.
if(c.length<2)
return c[0];
else
{
int m=0;
for(int i=0;i<c.length;i++)
{
for(int j=i+1;j<c.length;j++)
{
if(c[j]==c[i])
{
count++;
}
}
t[m++]=count;
}
int max=t[0];
int max_index=0;
for(int i=1;i<t.length;i++)
{
if(t[i]>=max) //used the logic for finding frequency.
{
max_index=i;
}
}
for(int l=0;l<c.length;l++)
{ //Return the position of the frequent element.
if(l==max_index)
{
break;
}
}
return max_index;
}
}
答案 2 :(得分:0)
也许这会有所帮助:
public int countFreq(int[] c){
int[] frequencyChart = new int[c.length]; //always give useful names for variables, it always helps.
if(c.length < 2){
return c[0];
}
else{
for(int i = 0; i < c.length; i++){//loops through array
for(int j = 0; j < c.length; j++){//nested loop that'll count frequencies.
if(c[i] == c[j]){
frequencyChart[i]++;
}
}
}
//this would have set up the array of frequencies, which corresponds with everything in c. Note: there will be duplicates of frequencies.
int maxFreq = frequencyChart[0];
int mostFreq = c[0];
for(int a = 0; a < frequencyChart.length; a++){//now to find the highest frequency...
if(frequencyChart[a] > maxFreq){
maxFreq = frequencyChart[a];
mostFreq = c[a];
}
}
return mostFreq;
}
希望这有帮助!
答案 3 :(得分:0)
此问题的先前答案解释了当前实施中的问题并为其提供了解决方案。我不会重复它们,但我想建议一个基于流媒体功能的Java 8风格解决方案。
首先,您需要一种方法来流式传输数字的数字。最简单的方法可能是扩展Spliterators.AbstractIntSpliterator
(请注意,此供应商会将最后一位数字返回到第一位。由于我们不关心此问题的顺序,因此):
public class ReverseDigitSupplier extends Spliterators.AbstractIntSpliterator {
private int number;
private ReverseDigitSupplier(int number) {
super(Long.MAX_VALUE, 0);
this.number = Math.abs(number);
}
@Override
public boolean tryAdvance(IntConsumer action) {
int digit = number % 10;
number /= 10;
action.accept(digit);
return number != 0;
}
}
一旦掌握了这种能力,你就可以做到以下几点(内联说明):
Integer commonDigit =
Arrays.stream(numbers) // stream the array
.flatMap(x -> StreamSupport.intStream
(new ReverseDigitSupplier(x), false)) // stream digits
.boxed() // convert int to Integer
.collect(Collectors.groupingBy
(Function.identity(),
Collectors.counting())) // Group to a cardinality map
.entrySet() // Take the entrySet() of the map
.stream() // stream it
.reduce(BinaryOperator.maxBy
(Map.Entry.comparingByValue())) // find the max value
.map(Map.Entry::getKey) // take the key
.orElse(null); // and terminate the optional
答案 4 :(得分:0)
给定一个数字数组作为输入,返回在输入中出现最大次数的数字。
这将对您有所帮助。它很简单,不使用任何构建功能。
public class MostFreqDigit {
static int[] testcase1 = {24,27,30,30,31,34,37,40,42,10,0};
public static void main(String args[]){
MostFreqDigit testInstance = new MostFreqDigit();
int result = testInstance.frequentDigit(testcase1);
System.out.println(result);
}
public int frequentDigit(int[] numbers){
//write your code here
int mostfreq=0;
int y=0;
char dig='\u0000';
String str="";
for(int i=0;i<numbers.length;i++){
str=str+numbers[i];
}
for(int j=0;j<str.length();j++){
char ch=str.charAt(j);
int c=exists(ch,str);
if(c>mostfreq){
mostfreq=c;
dig=ch;
y=ch-48;
}
else if(c==mostfreq){
if(dig>ch){
dig=ch;
y=ch-48;
}
}
}
return y;
}
public int exists(char c,String s){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==c)
count++;
}
return count;
}
}