Count repetitions in array in java [closed]












1















I know that similar questions have been asked and I have researched
many websites. I have tried to use some of the answers but my code is
still not working.



I am going through a previous assignment to help build my knowledge
of Java. Please forgive any errors in my code, I am still learning
Java.



Here is my question:




Implement a method count which, given an array of integer elements, returns another array containing the number of occurrences of each integer {0, ..., r} in the input array, where r is an integer to show the upper boundary of the integers that you need to count.



The returned array of counts will be of size r + 1, where the element at each index i corresponds to the number of occurrences of integer i (with i in {0, ..., r}).



Elements in the input array outside of the integer range from 0 to r can be ignored.



For example, given the input [0, 8, 1, 3, 1, 3, 10, 3] with r is 4, the output should be [1, 2, 0, 3, 0].



If the input array is null or of length 0, this will return null.



Space requirements: Method count should only use additional space for the count array.



Time requirements: The counts should be calculated in a single pass through the input array.




Here is what I've done so far, it doesn't meet the requirements so I need help in order to find the right solution:




public static int count(int arr, int r) {
int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < r; j++) {
if (arr[i] == j) {
count[i]++;

}

}
}
return count;
}









share|improve this question













closed as unclear what you're asking by ParkerHalo, GhostCat, cнŝdk, Nic3500, blue-phoenox Nov 19 '18 at 17:54


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Why doesn't it meet the requirements? What's the problem? Be specific.
    – nicomp
    Nov 19 '18 at 14:29










  • @nicomp Index out of bounds + I think the logic is wrong
    – Pablo Biedma
    Nov 19 '18 at 14:30










  • The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
    – Karan Khanna
    Nov 19 '18 at 14:34


















1















I know that similar questions have been asked and I have researched
many websites. I have tried to use some of the answers but my code is
still not working.



I am going through a previous assignment to help build my knowledge
of Java. Please forgive any errors in my code, I am still learning
Java.



Here is my question:




Implement a method count which, given an array of integer elements, returns another array containing the number of occurrences of each integer {0, ..., r} in the input array, where r is an integer to show the upper boundary of the integers that you need to count.



The returned array of counts will be of size r + 1, where the element at each index i corresponds to the number of occurrences of integer i (with i in {0, ..., r}).



Elements in the input array outside of the integer range from 0 to r can be ignored.



For example, given the input [0, 8, 1, 3, 1, 3, 10, 3] with r is 4, the output should be [1, 2, 0, 3, 0].



If the input array is null or of length 0, this will return null.



Space requirements: Method count should only use additional space for the count array.



Time requirements: The counts should be calculated in a single pass through the input array.




Here is what I've done so far, it doesn't meet the requirements so I need help in order to find the right solution:




public static int count(int arr, int r) {
int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < r; j++) {
if (arr[i] == j) {
count[i]++;

}

}
}
return count;
}









share|improve this question













closed as unclear what you're asking by ParkerHalo, GhostCat, cнŝdk, Nic3500, blue-phoenox Nov 19 '18 at 17:54


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Why doesn't it meet the requirements? What's the problem? Be specific.
    – nicomp
    Nov 19 '18 at 14:29










  • @nicomp Index out of bounds + I think the logic is wrong
    – Pablo Biedma
    Nov 19 '18 at 14:30










  • The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
    – Karan Khanna
    Nov 19 '18 at 14:34
















1












1








1








I know that similar questions have been asked and I have researched
many websites. I have tried to use some of the answers but my code is
still not working.



I am going through a previous assignment to help build my knowledge
of Java. Please forgive any errors in my code, I am still learning
Java.



Here is my question:




Implement a method count which, given an array of integer elements, returns another array containing the number of occurrences of each integer {0, ..., r} in the input array, where r is an integer to show the upper boundary of the integers that you need to count.



The returned array of counts will be of size r + 1, where the element at each index i corresponds to the number of occurrences of integer i (with i in {0, ..., r}).



Elements in the input array outside of the integer range from 0 to r can be ignored.



For example, given the input [0, 8, 1, 3, 1, 3, 10, 3] with r is 4, the output should be [1, 2, 0, 3, 0].



If the input array is null or of length 0, this will return null.



Space requirements: Method count should only use additional space for the count array.



Time requirements: The counts should be calculated in a single pass through the input array.




Here is what I've done so far, it doesn't meet the requirements so I need help in order to find the right solution:




public static int count(int arr, int r) {
int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < r; j++) {
if (arr[i] == j) {
count[i]++;

}

}
}
return count;
}









share|improve this question














I know that similar questions have been asked and I have researched
many websites. I have tried to use some of the answers but my code is
still not working.



I am going through a previous assignment to help build my knowledge
of Java. Please forgive any errors in my code, I am still learning
Java.



Here is my question:




Implement a method count which, given an array of integer elements, returns another array containing the number of occurrences of each integer {0, ..., r} in the input array, where r is an integer to show the upper boundary of the integers that you need to count.



The returned array of counts will be of size r + 1, where the element at each index i corresponds to the number of occurrences of integer i (with i in {0, ..., r}).



Elements in the input array outside of the integer range from 0 to r can be ignored.



For example, given the input [0, 8, 1, 3, 1, 3, 10, 3] with r is 4, the output should be [1, 2, 0, 3, 0].



If the input array is null or of length 0, this will return null.



Space requirements: Method count should only use additional space for the count array.



Time requirements: The counts should be calculated in a single pass through the input array.




Here is what I've done so far, it doesn't meet the requirements so I need help in order to find the right solution:




public static int count(int arr, int r) {
int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < r; j++) {
if (arr[i] == j) {
count[i]++;

}

}
}
return count;
}






java arrays loops count






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 14:23









Pablo Biedma

19111




19111




closed as unclear what you're asking by ParkerHalo, GhostCat, cнŝdk, Nic3500, blue-phoenox Nov 19 '18 at 17:54


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by ParkerHalo, GhostCat, cнŝdk, Nic3500, blue-phoenox Nov 19 '18 at 17:54


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Why doesn't it meet the requirements? What's the problem? Be specific.
    – nicomp
    Nov 19 '18 at 14:29










  • @nicomp Index out of bounds + I think the logic is wrong
    – Pablo Biedma
    Nov 19 '18 at 14:30










  • The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
    – Karan Khanna
    Nov 19 '18 at 14:34
















  • 1




    Why doesn't it meet the requirements? What's the problem? Be specific.
    – nicomp
    Nov 19 '18 at 14:29










  • @nicomp Index out of bounds + I think the logic is wrong
    – Pablo Biedma
    Nov 19 '18 at 14:30










  • The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
    – Karan Khanna
    Nov 19 '18 at 14:34










1




1




Why doesn't it meet the requirements? What's the problem? Be specific.
– nicomp
Nov 19 '18 at 14:29




Why doesn't it meet the requirements? What's the problem? Be specific.
– nicomp
Nov 19 '18 at 14:29












@nicomp Index out of bounds + I think the logic is wrong
– Pablo Biedma
Nov 19 '18 at 14:30




@nicomp Index out of bounds + I think the logic is wrong
– Pablo Biedma
Nov 19 '18 at 14:30












The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
– Karan Khanna
Nov 19 '18 at 14:34






The value of r you are taking is wrong. In your example the value of r should be 10 because the numbers in the array are from {0 ... 10}
– Karan Khanna
Nov 19 '18 at 14:34














1 Answer
1






active

oldest

votes


















2














You are really close, but seems maybe a small bit is wrong.



int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
if( arr[i] <= r) {
count[arr[i]]++;
}
}


I think the above will work, if you think about it, each element of arr corresponds to an index in count as long as that index is within {0...r}, so we check that the value is within that range, then we increment the integer at that index within count.






share|improve this answer





















  • @Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
    – The Vee
    Nov 19 '18 at 16:46




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You are really close, but seems maybe a small bit is wrong.



int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
if( arr[i] <= r) {
count[arr[i]]++;
}
}


I think the above will work, if you think about it, each element of arr corresponds to an index in count as long as that index is within {0...r}, so we check that the value is within that range, then we increment the integer at that index within count.






share|improve this answer





















  • @Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
    – The Vee
    Nov 19 '18 at 16:46


















2














You are really close, but seems maybe a small bit is wrong.



int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
if( arr[i] <= r) {
count[arr[i]]++;
}
}


I think the above will work, if you think about it, each element of arr corresponds to an index in count as long as that index is within {0...r}, so we check that the value is within that range, then we increment the integer at that index within count.






share|improve this answer





















  • @Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
    – The Vee
    Nov 19 '18 at 16:46
















2












2








2






You are really close, but seems maybe a small bit is wrong.



int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
if( arr[i] <= r) {
count[arr[i]]++;
}
}


I think the above will work, if you think about it, each element of arr corresponds to an index in count as long as that index is within {0...r}, so we check that the value is within that range, then we increment the integer at that index within count.






share|improve this answer












You are really close, but seems maybe a small bit is wrong.



int count = new int[r + 1];
for (int i = 0; i < arr.length; i++) {
if( arr[i] <= r) {
count[arr[i]]++;
}
}


I think the above will work, if you think about it, each element of arr corresponds to an index in count as long as that index is within {0...r}, so we check that the value is within that range, then we increment the integer at that index within count.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 14:33









Kyle Wilson

362




362












  • @Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
    – The Vee
    Nov 19 '18 at 16:46




















  • @Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
    – The Vee
    Nov 19 '18 at 16:46


















@Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
– The Vee
Nov 19 '18 at 16:46






@Pablo ...This, ideally, but as a minimal change to your original logic just fix count[i]++ to count[j]++, because j is equal to arr[i] at that point. Probably what you wanted, just a typo.
– The Vee
Nov 19 '18 at 16:46





Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory