find the frequency of elements in a java array

后端 未结 9 1567
灰色年华
灰色年华 2020-12-03 16:40

I have an int array:

{1,2,4,2,3,5,6,4,3}

How can I find frequencies of array elements like 1=1,2=2,3=2,4=4... I need a class w

9条回答
  •  余生分开走
    2020-12-03 16:51

    You have to do a few things:

    1. Define an upper and lower bound for your range of numbers.
    2. Establish a convenient object/data structure to store occurrences of these numbers.
    3. Iterate through the passed-in array and count all occurrences of each number, storing the result in the convenient object/data structure.

    If this is done in a simple manner, it could be only a matter of reading the elements from the passed-in array and printing out the final result.

提交回复
热议问题