在数字数组中查找数字的 maxFreq 的输出未按预期出现
Output of finding maxFreq of digit in an array of numbers does not come as expected
Given an array of numbers as input, return the digit which occurs the maximum number of times in the input.
我的方法
我首先将数组的每个成员的每个数字分开。然后,我计算了每个数字的频率,然后我找到了该数字出现的最大次数并记下了它的位置。当我搜索数字分隔数组中的位置时,我找到了出现次数最多的数字。
这是我尝试使用以下代码执行的操作:
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;
}
}
}
让你的函数return只有变量而不是它的计数
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;
}
}
你可以简化这个算法的方法:
- 创建一个
int[] counts = new int[10]
,最多10位
- 对于输入中的每个数字
- 每个数字
- 增加位数
- 找到最大元素,return它的索引,就是你要找的数字
例如:
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();
注意:当有多个数字具有相同的计数时,此实现将return较小的数字。
也许这会有所帮助:
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;
}
希望对您有所帮助!
这个问题之前的回答解释了当前实现中的问题并提供了解决方案。我不会重复它们,但我想根据流媒体功能建议 Java 8 种样式的解决方案。
首先,您需要一种流式传输数字数字的方法。最简单的方法可能是扩展 Spliterators.AbstractIntSpliterator
(请注意,此供应商 returns 从最后一位数字到第一位数字。因为我们不关心这个问题的顺序,它足够了):
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
Given an array of numbers as input, return the digit which occurs the maximum number of times in the input.
这将对您有所帮助。它很简单,而且不使用任何内置函数。
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;
}
}
Given an array of numbers as input, return the digit which occurs the maximum number of times in the input.
我的方法
我首先将数组的每个成员的每个数字分开。然后,我计算了每个数字的频率,然后我找到了该数字出现的最大次数并记下了它的位置。当我搜索数字分隔数组中的位置时,我找到了出现次数最多的数字。
这是我尝试使用以下代码执行的操作:
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;
}
}
}
让你的函数return只有变量而不是它的计数
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;
}
}
你可以简化这个算法的方法:
- 创建一个
int[] counts = new int[10]
,最多10位 - 对于输入中的每个数字
- 每个数字
- 增加位数
- 找到最大元素,return它的索引,就是你要找的数字
例如:
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();
注意:当有多个数字具有相同的计数时,此实现将return较小的数字。
也许这会有所帮助:
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;
}
希望对您有所帮助!
这个问题之前的回答解释了当前实现中的问题并提供了解决方案。我不会重复它们,但我想根据流媒体功能建议 Java 8 种样式的解决方案。
首先,您需要一种流式传输数字数字的方法。最简单的方法可能是扩展 Spliterators.AbstractIntSpliterator
(请注意,此供应商 returns 从最后一位数字到第一位数字。因为我们不关心这个问题的顺序,它足够了):
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
Given an array of numbers as input, return the digit which occurs the maximum number of times in the input.
这将对您有所帮助。它很简单,而且不使用任何内置函数。
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;
}
}