Counting repeated elements in an integer array

前端 未结 13 1289
挽巷
挽巷 2020-11-27 08:02

I have an integer array crr_array and I want to count elements, which occur repeatedly. First, I read the size of the array and initialize it with numbers read

13条回答
  •  独厮守ぢ
    2020-11-27 08:11

    You have to use or read about associative arrays, or maps,..etc. Storing the the number of occurrences of the repeated elements in array, and holding another array for the repeated elements themselves, don't make much sense.

    Your problem in your code is in the inner loop

     for (int j = i + 1; j < x.length; j++) {
    
            if (x[i] == x[j]) {
                y[i] = x[i];
                times[i]++;
            }
    
        }
    

提交回复
热议问题