find the frequency of elements in a java array

后端 未结 9 1522
灰色年华
灰色年华 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 17:05

    Without giving it away here is a good starting point:

    int[] array = {1,2,4,2,3,5,6,4,3};
    
            public int[] (array){
                //need to perform a sort...or a search
                //after searching check for matches,
                //sorting could make performing comparisons more efficient
                //not all searches/sorts are created equal.
    
                int[array.length] result += {"["+numberChecked+"]="+freqOccurred};
                return result;
            }
    

    This code is has not been compiled so think of it more as psuedocode. The purpose is to get you thinking about how to achieve the desired goal. An java package may already exist that can check the frequency elements in an array, but this is what you are most likely looking for. Good Luck.

提交回复
热议问题