import java.util.*;
public class Test {
public static void main(String[] args)
{
int[] number = new int[50];
int index = 0;
boolean swap = true;
int temp;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Number: ");
System.out.println(" ");
do
{
int input = keyboard.nextInt();
if (input != -999)
number[index++] = input;
else
break;
} while (index != 0);
int[] newNumbers = new int[index];
for (int i = 0; i < index; i++)
newNumbers[i] = number[i];
System.out.println("\nNumbers\t" + "Occurances");
goBack: for (int i = index - 1; i >= 0; i--)
{
for (int n = index - 1; n > i; n--)
if (newNumbers[n] == newNumbers[i])
continue goBack;
int count = 0;
for (int n = 0; n < index; n++)
if (newNumbers[n] == newNumbers[i])
count++;
for(int s=0; s < newNumbers.length-1; s++){
for(int j=1; j < newNumbers.length-s; j++){
if(newNumbers[j-1] > newNumbers[j]){
temp=newNumbers[j-1];
newNumbers[j-1] = newNumbers[j];
newNumbers[j] = temp;
}
}
}
System.out.println( newNumbers[i] + " " + count);
}
}
}
The code is intended to take the input through the keyboard scanner. The entered integers are compared and a list of distinct elements of the array number[] will be sorted and printed. However, The list of input contains multiples of some elements. The elements that are repeated are marked in a count. The final output should be a list of the distinct array elements (no duplicates) in order from greatest to least with their respective counts.
The input is as follows: -12, 3, -12, 4, 1, 1, -12, 1, -1, 1, 2, 3, 4, 2, 3, -12 when the sort is through and this prints, the index 4 has a count of four when it should have a count of 2. I have tried selection, bubble, and exchange sort algorithms all with similar results. Any advice would be greatly appreciated :) .
Hope this helps , though can be a better solution than this :
public static void main(String[] args) {
int[] numbers = new int[50] ;
int index = 0;
int temp;
Scanner keyboard = new Scanner(System.in);
// get the user input
System.out.print("Enter Number: ");
System.out.println(" ");
do {
int input = keyboard.nextInt();
if (input != -999)
numbers[index++] = input;
else
break;
} while (index != 0);
keyboard.close();
System.out.println("\nNumbers\t" + "Occurances");
// create a new array and store the user input
int[] newNumbers = new int[index];
for (int i = 0; i < index; i++)
newNumbers[i] = numbers[i];
// sort the array
for (int s = 0; s < newNumbers.length - 1; s++) {
for (int j = 1; j < newNumbers.length - s; j++) {
if (newNumbers[j - 1] < newNumbers[j]) {
temp = newNumbers[j - 1];
newNumbers[j - 1] = newNumbers[j];
newNumbers[j] = temp;
}
}
}
System.out.println(Arrays.toString(newNumbers));
int count = 1;
int prevElement = 0;
if (newNumbers.length > 0) {
prevElement = newNumbers[0];
}
// print the results
for (int x = 1; x < newNumbers.length; x++) {
if (newNumbers[x] == prevElement) {
count++;
} else {
System.out.println(prevElement + " occurs " + count + "times");
prevElement = newNumbers[x];
count = 1;
}
}
System.out.println(prevElement + " occurs " + count + "times");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With