Determining the frequency of unique numbers

I am trying to solve a problem in Java as part of my job. The problem is as follows:

The user enters ten numbers one by one upon request on the screen. The screen then assigns a different value to the array and a similar array to keep the frequency of how many times these numbers appear.

I did the following work, but it seems like I'm stuck somewhere in assigning frequencies and different values โ€‹โ€‹to arrays:

import java.util.*;

public class JavaApplication10 
{
    public static void main(String[] args)
    {
       int [] numbers = new int [10];
       int [] count = new int[10];
       int [] distinct = new int[10];

       for (int k=0;k<10;k++)
       {
           count[k]=0;
           distinct[k]=0;
       }
       java.util.Scanner input = new java.util.Scanner(System.in);

       System.out.print("Enter number 0: ");
       numbers[0]=input.nextInt();
       count[0]=1;
       distinct[0]=numbers[0];
       int j=0;
       for (int i = 1;i<10;i++)
       {
           System.out.print("Enter number "+i+": ");
           numbers[i]=input.nextInt();

           while(j<i)
           {
               if (distinct[j]==numbers[i])
               count[j]=count[j]+1;
               else
                   distinct[j+1]=numbers[i];
               j++;
           }
       }
    for (int k=0;k<10;k++)
    {
        System.out.println(distinct[k]+ " "+count[k]);
    }


       }
   }

I know that it is unfair to ask someone to help me solve a problem. But any hint will be helpful. thank you

+4
source share
6 answers

- numbers limited to 0-9? If so, I would just complete the task.

( , "input" ):

[0] = ; [] ++;

for "0", for.

.

, !

+1

HashMap

: 1)

2) , hashmap , .

3),

4) 1

2

1)

2) ,

3),

4) freq [index] = 1

0

:

public Map<Integer, Integer> getFrequencies(Iterable<Integer> numbers) {
    Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>();
    for(Integer number : numbers) {
        if (frequencies.get(number) == null) {
            frequencies.put(number, 0);
        }
        frequencies.put(number, frequencies.get(number) + 1);
    }
    return frequencies;
}

number -> frequency.

Java, , . . Java, 25: .

0

Scanner, , , .

    int[] numbers = { 1, 2, 2, 2, 3, 3, 3, 1, 1, 2 };
    int[] count = new int[10];
    int[] distinct = new int[10];

    count[0] = 1;
    distinct[0] = numbers[0];
    int disPos = 1; //Current possition in the distinct array
    boolean valueInarray = false;
    for (int i = 1; i < 10; i++) {
        valueInarray = false;
        for (int d = 0; d < i; d++) {

            if (numbers[i] == distinct[d]) {
                count[d] = count[d] + 1;
                valueInarray = true;
                break;
            }

        }
        if (!valueInarray) {
            distinct[disPos] = numbers[i];

            count[disPos] = 1;
            disPos++;
        }

    }
0

.. ...

import java.util.Scanner;
import java.util.Arrays;

public class JavaApplication10 
{
  public static void main(String[] args)
  {
   int [] numbers = new int [10];
   int [] count = new int[10];
   int [] distinct = new int[10];
   int [] distinct1 = new int[1];
   int distinctCount = 0;
   boolean found = false;

   Scanner input = new Scanner(System.in);

   for (int i=0; i<10; i++) {
   found = false;
   System.out.print("Enter number " + i);
   numbers[i]=input.nextInt(); //Add input to numbers array

   for (int j=0; j<=distinctCount; j++)
   {
     if (distinct1[j] == numbers[i]){ // check to see if the number is already in the distinct array

           count[j] = count[j] + 1; // Increase count by 1
           found = true; 
           break;
     }
   }

   if (!found) {
       distinct[distinctCount] = numbers[i];
       count[distinctCount] = 1;
       distinctCount++;
       distinct1 = Arrays.copyOf(distinct, distinctCount+1);
   }

   }
   for (int j=0; j<distinctCount; j++) 
     System.out.println("The number " + distinct1[j] + " occurs " + count[j] + " times" );


}

}

0

, , , , ...

import java.util.HashMap;
import java.util.Scanner;

public class JavaApplication10 {
public static void main(String[] args) {
    // Initializing variables
    int[] numbers                       = new int[10];
    HashMap<Integer, Integer> table     = new HashMap<Integer, Integer>();
    Scanner input                       = new Scanner(System.in);

    // Getting the 10 inputs
    for(int x=0; x<10; x++) {

        // Asking for input
        System.out.println("Enter number "+x+":");
        numbers[x]=input.nextInt();

        // If the table contains the number, add 1
        // Otherwise: set value to 1
        if(table.containsKey(numbers[x]))
            table.put(numbers[x], table.get(numbers[x])+1);
        else
            table.put(numbers[x],1);

    }
    // Closing the reader
    input.close();      

    // Get the highest and smallest number
    int highest=0;
    int smallest=0;
    for(int i:table.keySet()) {
        if(i>highest)
            highest=i;
        if(i<smallest)
            smallest=i;
    }



    // For every value between the smallest and the highest
    for (int x=smallest; x<=highest; x++) {
        // Check if the frequency > 0, else continue
        if(table.get(x)==null)
            continue;
        // Output
        System.out.println(x+" is "+table.get(x)+" times in \'frequence\'");
    }

}
}

This also applies to negative numbers, unlike other codes. If you do not want to use HashMaps, let me know so that I can create something with arrays.

Let me know if this (does not work)!
Happy coding (and good luck with your job);) -Charlie

0
source

All Articles