How do I count the number of occurrences in an array?

后端 未结 2 519
旧巷少年郎
旧巷少年郎 2021-01-21 08:31

I have generated an array of 5 random integers from 1-5. Here is what the array looks like now: myArray[5] = {3, 3, 1, 4, 5}

I have now sorted the array of

2条回答
  •  借酒劲吻你
    2021-01-21 08:58

    Make a hash and initialize with zero.

    int hash[10000]={0};
    for(int i=0;i

    the hash at index arr[i] will hold value which is the count of occurrence of that number. As hash[arr[i]]++ will increment the count at index equal to the value of arr[i]. This way we can check which value occurred how many times by checking hash[arr[i]] where arr[i] is value to be checked.

提交回复
热议问题