A function that checks for how many times each element appears in an array

后端 未结 3 612
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 13:15

I am making a function that lists an array and says how many times each element appears.

What I have thought of on my own so far is i should loop through the array and t

3条回答
  •  失恋的感觉
    2021-01-29 13:33

    I have posted a pseudocode, to help you with the algorithm which you said you are unable to understand. I hope this will encourage you to start the code.

    //store the first number in a variable and find 
    //how many times it occurs in the array, repeat this for all
    //the elements in the array
    
    int current = arr[0];  
    int i=0, count=0;  
    
    for(i=0; i < arr.length;i++){ // loop through all the elements of the array
    
         if(arr[i]==current)   {
             count++;  // if current number is same as the number you are looking for
                       // then increment the counter
       } // end of if-loop
    }
    

提交回复
热议问题