Results 1 to 3 of 3
  1. #1
    Npcomplete is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default Counting Duplicate Variables in an Array

    I have a program that sorts integers in an array. I would like for the last method, printcount, to count the number of occurences of each element, but I don't know how. Any help would be much appreciated. I'm also having problems making a partially filled array, so that the array will not initialize and print the leftover elements.

    Java Code:
    package sortintegers;
    
    import java.util.Scanner;
    
    public class Main
    {
    
    public static void main(String[] args)
    {
    
    int maxNumberElements = 50;
    
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter number of elements you want to enter:");
    int input = reader.nextInt();
    int index = 0;
    
    System.out.println("Now enter in integers:");
    int[] num = new int[maxNumberElements];
    
    for(int i = 0; i < input; i++)
    {
        System.out.print("Enter integer: ");
        num[index] = maxNumberElements;    
        index++;                                    
        num[i] = reader.nextInt();
        
    }
    selectionSort(num);
    printArray(num);
    }
    
    private static void printArray(int[] a)
    {
        for (int i : a)
        System.out.print(i + " ");
        System.out.println("");
    }
    
    private static void selectionSort(int[] a)
    {
        for (int i = 0; i < a.length - 1; i++)
        {
            int minIndex = findMaximum(a, i);
            if (minIndex != i)
            swap(a, i, minIndex);
        }
    }
    
    private static int findMaximum(int[] a, int first)
    {
        int minIndex = first; 
    
        for (int i = first + 1; i < a.length; i++)
        if (a[i] > a[minIndex])
        minIndex = i;
    
        return minIndex;
    }
    
    
    private static void swap(int[] a, int x, int y)
    {
        int temp = a[x];
        a[x] = a[y];
        a[y] = temp;
    } 
    private static void printCount(int a[])
    {
        int count = 0;
        int store;
        for (int i = count + 1; i < a.length; i++)
            a[i] = store;
        
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    To design your counting code decide How you are going to store and report the counts?
    One way would be to use a hashtable with the key being the item being counted and the value being the count.

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM
  3. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 06:27 AM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •