Results 1 to 3 of 3
 10242008, 08:42 AM #1Member
 Join Date
 Oct 2008
 Posts
 3
 Rep Power
 0
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; } }
 10242008, 09:01 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
Simply count each element at a time and go through the whole array.
 10242008, 08:33 PM #3
Similar Threads

Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05062011, 04:03 PM 
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10222008, 02:33 PM 
Counting the number of columns in a 2D array,
By KalEl in forum New To JavaReplies: 9Last Post: 10212008, 06:27 AM 
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11292007, 06:51 PM
Bookmarks