Print occurrences of elements in an array list [closed]
I need two input array A and array B.
The answer will be like this - if input A is
{5,2,6,8}
and input B is
{6,5,5,8,5,6}
then the output will be:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
java
closed as off-topic by Thomas, user6910411, Joakim Danielson, vanje, Nicholas K Nov 12 at 16:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user6910411, Joakim Danielson, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I need two input array A and array B.
The answer will be like this - if input A is
{5,2,6,8}
and input B is
{6,5,5,8,5,6}
then the output will be:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
java
closed as off-topic by Thomas, user6910411, Joakim Danielson, vanje, Nicholas K Nov 12 at 16:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user6910411, Joakim Danielson, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
8
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
1
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38
add a comment |
I need two input array A and array B.
The answer will be like this - if input A is
{5,2,6,8}
and input B is
{6,5,5,8,5,6}
then the output will be:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
java
I need two input array A and array B.
The answer will be like this - if input A is
{5,2,6,8}
and input B is
{6,5,5,8,5,6}
then the output will be:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
java
java
edited Nov 12 at 11:31
user6910411
32.6k86995
32.6k86995
asked Nov 12 at 10:44
Troy Le
32
32
closed as off-topic by Thomas, user6910411, Joakim Danielson, vanje, Nicholas K Nov 12 at 16:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user6910411, Joakim Danielson, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Thomas, user6910411, Joakim Danielson, vanje, Nicholas K Nov 12 at 16:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user6910411, Joakim Danielson, Nicholas K
If this question can be reworded to fit the rules in the help center, please edit the question.
8
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
1
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38
add a comment |
8
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
1
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38
8
8
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
1
1
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38
add a comment |
1 Answer
1
active
oldest
votes
Here is an example of how you can achieve it.
public static void main(String args) {
int A={5,2,6,8},
B={6,5,5,8,5,6};
int count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is an example of how you can achieve it.
public static void main(String args) {
int A={5,2,6,8},
B={6,5,5,8,5,6};
int count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
add a comment |
Here is an example of how you can achieve it.
public static void main(String args) {
int A={5,2,6,8},
B={6,5,5,8,5,6};
int count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
add a comment |
Here is an example of how you can achieve it.
public static void main(String args) {
int A={5,2,6,8},
B={6,5,5,8,5,6};
int count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}
Here is an example of how you can achieve it.
public static void main(String args) {
int A={5,2,6,8},
B={6,5,5,8,5,6};
int count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}
answered Nov 12 at 11:58
Whole Brain
138111
138111
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
add a comment |
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
1
1
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
Thank for your advice, i will try this and feedback ^^
– Troy Le
Nov 12 at 15:06
add a comment |
8
Welcome to SO, please take a tour and read How to Ask. In advance, I'll tell you this: SO is not a "write my code" site, i.e. you have to show some effort, especially some code and how you've already tried to solve your problem, otherwise the question will receive downvotes and is very likely to get closed.
– Thomas
Nov 12 at 10:45
1
And be clearer what you want to achieve. As I understand you want to count the occurences of the elements of A in B.
– Ralf Renz
Nov 12 at 10:51
convert the ArrayList into an array,and use counting sort technique on it.Next time please show us what you have tried or what problems you were facing
– Gaurav Bahadur
Nov 12 at 11:38