Advertisement
brilliant_moves

Mode.java

Nov 30th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.94 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Mode {
  4.  
  5.     // find 1 or more modes from an array
  6.     // the mode is the most repeated successive number
  7.     // there can be more than one mode
  8.  
  9.     /**
  10.     *   Program:    Modes.java
  11.     *   Purpose:    Yahoo! Answers
  12.     *   Creator:    Chris Clarke
  13.     *   Created:    30.11.2013
  14.     */
  15.  
  16.     public static void main(String[] args) {
  17.         // test data
  18.         int[] num = {2,2,3,4,7,7,7,8,8,8}; // removed an 8 - for test purposes
  19.  
  20.         ArrayList<Integer> numberList    = new ArrayList<>(); // number
  21.         ArrayList<Integer> frequencyList = new ArrayList<>(); // frequency
  22.         ArrayList<Integer> modeList      = new ArrayList<>(); // 1 or more max repeats
  23.  
  24.         int repeats = 1;    // starts with 1 of current number
  25.         int max = 1;        // max is 1 to begin with
  26.  
  27.         for (int x=0; x < num.length; x++) {
  28.             if (x < num.length-1) {
  29.                 if (num[x] == num[x+1]) { // a repeated number
  30.                     if (++repeats > max) {
  31.                         max = repeats;
  32.                     } // end if repeats
  33.                 } else {
  34.                     if (!numberList.contains(num[x])) {
  35.                         numberList.add(num[x]); // add the number to list
  36.                         frequencyList.add(repeats); // add frequency to list
  37.                     } // end if
  38.                     repeats = 1; // new number, not repeat
  39.                 } // end if num
  40.             } else {
  41.                 if (!numberList.contains(num[x])) {
  42.                     numberList.add(num[x]); // add the number to list
  43.                     frequencyList.add(repeats); // add frequency to list
  44.                 } // end if
  45.                 repeats = 1;
  46.             } // end if x
  47.  
  48.         } // end for
  49.  
  50.         // find all numbers whose frequency = max
  51.         for (int i=0; i<frequencyList.size(); i++) {
  52.             if (frequencyList.get(i) == max) {
  53.                 modeList.add(numberList.get(i));
  54.             } // end if
  55.         } // end for
  56.  
  57.         // output result(s)
  58.         System.out.print("The numbers are ");
  59.         for (int i: num) {
  60.             System.out.printf("%d ", i);
  61.         } // end for
  62.  
  63.         System.out.println("\nnumberList = "+numberList);
  64.         System.out.println("frequencyList = "+frequencyList);
  65.         System.out.println("Mode(s) = "+modeList);
  66.     } // end main()
  67. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement