Reputation: 3
I tried to write a program to determine the number of repeated integers in an array and their counts but the program outputs roughly fulse, so my question is what the problem in my code i do not want use any class , thanks.
for example :
Input: Enter array size: 11 Enter array elements: 13 34 22 4 499 4 22 18 4 1 1 Output: There are 3 repeated numbers: 22: 2 times 4: 3 times 1: 2 times
but my output is :
1:2 times 4:3 times 4:2 times 22:2 times
import java.util.Scanner;
public class repeated_elements {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.nextInt();
// The Array
int[]Array = new int[size];
// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for(int i =0; i<size; i++){
Array[i]=input.nextInt();
}
// Sorting The Array (Ascending Order)
for(int j = 0; j<size; j++){
int mini = Array[j];
int mini_index = j;
for(int i = j; i<size; i++){
if(Array[i] < mini){
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}
// Count The Repeated Numbers
for(int i=0; i<size; i++){
int key = Array[i];
int counter = 0;
for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}
}
if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
}
}
Upvotes: 0
Views: 4191
Reputation: 356
When we take a key for finding the ocurrence, first we need to check whether it is already searched for occurence (eg.4). So i have kept the key in array without any duplicates and then start searching it. It can be easily done with collections methods..
public class RepeatedElements {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.nextInt();
// The Array
int[] Array = new int[size];
// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for (int i = 0; i < size; i++) {
Array[i] = input.nextInt();
}
// Sorting The Array (Ascending Order)
for (int j = 0; j < size; j++) {
int mini = Array[j];
int mini_index = j;
for (int i = j; i < size; i++) {
if (Array[i] < mini) {
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}
// System.out.println("sorted array:"+Arrays.asList(Array));
int key[] = new int[Array.length];
int keylen = 0;
// Count The Repeated Numbers
for (int i = 0; i < size; i++) {
int found = 0;
int k;
for ( k=0;k<keylen;k++){
if (key[k]==Array[i]){
found=1;
}
}
// 11 System.out.println("k value"+k+"i value"+i);
if (found==1)
continue;//already counted
else
key[keylen]=Array[i];
int counter = 0;
for (int j = i; j < size; j++) {
if (key[keylen] == Array[j]) {
counter++;
}
}
keylen++;
if (counter > 1) {
System.out.println(Array[i] + ":" + counter + " times ");
}
}
}
}
The output is attached in the screenshot.
Upvotes: 0
Reputation: 5423
instead of this piece of code :
// Count The Repeated Numbers
for(int i=0; i<size; i++){
int key = Array[i];
int counter = 0;
for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}
}
if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
try this:
// Count The Repeated Numbers
for(int i=0; i<size; ){ //<--- note the removal of i++
int key = Array[i];
int counter = 0;
for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}else{
i=j;
break;
}
}
if(counter > 1 ){
System.out.println(key+":"+counter+" times ");
}
}
the problem is you don't sync your i
and j
variable
so for an input like :
1 1 1 1
you will get
1:4
1:3
1:2
After the correction I tested the program and for input:
11
13 34 22 4 499 4 22 18 4 1 1
I get :
1:2 times
4:3 times
22:2 times
EDIT:
AS @CabelB suggested. it'll worth while have a look at Map
implementation as well.
Upvotes: 1
Reputation: 481
You parse each time the whole array, so you have to skip already parsed items.
Increment the i
counter in last for by counter
value:
if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
i +=counter;
}
That should be enough, hope it helps
Upvotes: 0
Reputation:
Your code seems to be ok, but for one small problem. When you are outputting the number of appearances, you are going through the entire array every time, regardless if the value currently being checked was already checked before. In short, for an input array of [1, 1, 1, 1, 2, 2, 2, 2, 3, 3], the output is 1:4 times, 1:3 times and 1:2 times and so on.
I think you need to separate the distinct values from the sorted array or at least make sure that you do not repeat the counting for values that have already been counted.
Upvotes: 0
Reputation: 3829
If I were you, I would solve this with Map and counting there, but your code corrected:
public class Rep {
public static void main(String[] args) {
int[] input = {1,1,2,2,2,0};
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.length;
// The Array
int[]Array = new int[size];
// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for(int i =0; i<size; i++){
Array[i]=input[i];
}
// Sorting The Array (Ascending Order)
for(int j = 0; j<size; j++){
int mini = Array[j];
int mini_index = j;
for(int i = j; i<size; i++){
if(Array[i] < mini){
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}
// Count The Repeated Numbers
for(int i=0; i<size; i++){
int key = Array[i];
int counter = 0;
for(int j=0; j<size; j++){
if(key == Array[j]){
counter++;
}
}
if(counter > 0 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
}
}
Upvotes: 0