Reputation: 109
Here I have sample java code which to count number of duplicate numbers in an array.
example:
I have array A[]={3,4,5,3,4,3}
so I need the output
3 occurred 3 times
4 occurred 2 times
5 occurred 1 time
How can I get this with my following program.
public class testNumberCount {
public static void main(String[] args) {
String[] temp;
System.out.println("Enter numbers Separated with comma(,)");
String inputSrc = "";
try {
Scanner sc = new Scanner(System.in);
inputSrc = sc.nextLine();
} catch (Exception ex) {
System.out.println("Exception:" + ex.getMessage());
}
temp = inputSrc.split(",");
System.out.println(Arrays.toString(temp));
int[] numberarray = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
numberarray[i] = Integer.parseInt(temp[i]);
}
for (int j = 0; j < numberarray.length; j++) {
for (int k = 0, count = 0; k < numberarray.length; k++) {
if (numberarray[j] == numberarray[k]) {
count++;
}
System.out.println("number " + numberarray[k] + " Occured:" + count + " times.");
}
}
}
}
here my wrong output is:
number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.
number 3 Occured:0 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:2 times.
number 3 Occured:2 times.
number 3 Occured:0 times.
number 4 Occured:0 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 3 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.
number 3 Occured:0 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:2 times.
number 3 Occured:2 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.
For this one I'm getting some wrong output, can anybody run and solve my problem.
Your help will be appreciated.
Upvotes: 1
Views: 134
Reputation: 1500245
You're currently writing output on each iteration of the inner loop... which means you're getting much more output than you want. You want to print out the result once per iteration of the outer loop, which means bringing count
into the scope of that loop too, and not using k
in the output either. So here's a start:
class Test {
public static void main(String[] args) throws Exception {
int[] numberarray = { 3, 4, 5, 3, 4, 3 };
for (int j = 0; j < numberarray.length; j++) {
int count = 0;
for (int k = 0; k < numberarray.length; k++) {
if (numberarray[j] == numberarray[k]) {
count++;
}
}
System.out.println("Number " + numberarray[j]
+ " occurred " + count + " times.");
}
}
}
The output of this is:
Number 3 occurred 3 times.
Number 4 occurred 2 times.
Number 5 occurred 1 times.
Number 3 occurred 3 times.
Number 4 occurred 2 times.
Number 3 occurred 3 times.
Now, that's accurate - but includes duplicates. Think about how you can detect duplicates (think about the relationship between j
and k
when you've spotted that numberarray[j] == numberarray[k]
).
An alternative is to go through the array once, maintaining a Map<Integer, Integer>
where the key is the value in the array, and the value is the number of times you've seen that value... but you might want to get the brute force version working first.
Upvotes: 2