需要在排序数组中打印频率。我的解决方案有错误,如何修复?

huangapple go评论72阅读模式
英文:

Have to print frequency in sorted array. My solution have bug, How to fix it?

问题

我必须计算一个排序数组中一个整数出现的次数。

期望的输入:

int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45}

期望的输出:

10 - 3
20 - 2
40 - 2
45 - 2

所以,我使用了一个从第一个(i)索引开始的for循环,将它与(i-1)索引(即0索引)的元素进行比较,如果相等,则递增计数器的值,如果不相等,则打印答案并将计数器的值设为1。

但是这个解决方案没有给我输出arr[arr.length - 1]元素的结果。为什么?

我的输出:

10 - 3
20 - 2
40 - 2

我的解决方案:

public static void main(String[] args) {
    // 一个排序数组中的频率
    int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45};
    get_Frequencies(arr);
}

public static void get_Frequencies(int[] arr) {
    int counter = 1;
    for(int i = 1; i < arr.length; i++) {
        if (arr[i] == arr[i - 1]) {
            counter++;
        }

        if (arr[i] != arr[i - 1]) {
            System.out.println(arr[i - 1] + " - " + counter);
            counter = 1;
        }
    }
}

如果您有任何其他问题,请告诉我。

英文:

I have to count how many times an integer is occurring in a sorted array.

Expected Input:

int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45}

Expected Output:

10 - 3

20 - 2

40 - 2

45 - 2

So, I used for loop which is starting from first(i) index and comparing it with((i-1)which is 0 index) element if it is equal then increment value of counter if it is not equal then printing answer and set value of counter at 1.

But this solution is not giving me output of arr[arr.length - 1] element. Why?

My Output:

10 - 3

20 - 2

40 - 2

My Solution -

public static void main(String[] args) {
    //Frequencies in a sorted array
    int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45};
    get_Frequencies(arr);
}

public static void get_Frequencies(int[] arr) {
    int counter = 1;
    for(int i = 1; i &lt; arr.length; i++) {
        if (arr[i] == arr[i - 1]) {
            counter++;
        }

        if (arr[i] != arr[i - 1]) {
            System.out.println(arr[i - 1] + &quot; - &quot; + counter);
            counter = 1;
        }
    }
}

答案1

得分: 0

自从您退出循环以后,最后一个整数的计数将不会被打印出来。您必须在退出循环后打印它。

代码 -

public class HelloWorld{

 public static void main(String[] args) {
  //Frequencies in a sorted array
  int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45};
   get_Frequencies(arr);
}

public static void get_Frequencies(int[] arr) {
   int counter = 1;
   int i = 1;
   for(; i < arr.length; i++) {
     if (arr[i] == arr[i - 1]) {
        counter++;
     }

     if (arr[i] != arr[i - 1]) {
       System.out.println(arr[i - 1] + " - " + counter);
       counter = 1;
    }
  }

  System.out.println(arr[i - 1] + " - " + counter);
 }
}
英文:

Since you exited the loop, the count of last integer won't be printed. You have to print it after exiting the loop.

Code -

public class HelloWorld{

 public static void main(String[] args) {
  //Frequencies in a sorted array
  int[] arr = {10, 10, 10, 20, 20, 40, 40, 45, 45};
   get_Frequencies(arr);
}

public static void get_Frequencies(int[] arr) {
   int counter = 1;
   int i = 1;
   for(; i &lt; arr.length; i++) {
     if (arr[i] == arr[i - 1]) {
        counter++;
     }

     if (arr[i] != arr[i - 1]) {
       System.out.println(arr[i - 1] + &quot; - &quot; + counter);
       counter = 1;
    }
  }

  System.out.println(arr[i - 1] + &quot; - &quot; + counter);
 }
}

huangapple
  • 本文由 发表于 2020年8月4日 23:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定