Results 1 to 3 of 3

Thread: Calculating the Mode

  1. #1
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts

    Calculating the Mode

    I need to calculate the mode of 50 numbers and im struggling on how to go abouts doing this. Mathematically i have no problem but writing a program to do this is another matter.
    This is what im thinking:

    first of all, is there a java function that can do this????

    • store the given numbers in an array
    • sort the array
    • compare a value in the array to the next value in the array (incrementing array[value1] and array[value2] accordingly)
    • where value1 == value2 increment x (incrementing array[value1] and array[value2] accordingly)
    • until end of array
    • print (x+1)


    I know this pseudo is very vague but i hope its enough to get my point accross.

    Thanks,
    nem.

  2. #2
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    You could use a HashMap. Store the number in the array as the index of the HashMap entry, and then increment it every time it is found in the array. So

    HashMap numbers = new HashMap();

    for (i=0; i < array.size; i++) {
    Integer thisNumber = numbers.get(array[i]);
    if (thisNumber == null) {
    numbers.put(new Integer(array[i], new Integer(1));
    } else {
    thisNumber = new Integer(thisNumber.intValue() + 1);
    }
    }

    Then just enumerate through the hashmap looking for the entry with the largest value.

    API for the HashMap:
    http://java.sun.com/j2se/1.3/docs/ap...l/HashMap.html

    Sorry - it's a bit rushed as I have to go out. Let me know if you need more help.
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  3. #3
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    i use mode calculation in my 3rd year project, which is ansi c

    convert the following idea to a more friendly javatastic form: a tally chart

    page through all the values. if the value has been encountered before increment its value in a tally chart, else add it to the bottom of the chart. then parse the chart with an ultra-simple if(entry[n+1]>entry[n]) largest=entry[n+1];

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Chassis fan doesn't stop in standby mode
    By coli in forum PC Hardware and Components
    Replies: 2
    Last Post: 15-02-2004, 01:35 AM
  2. boot windows nt workstation 4.0 to dos mode
    By Drunken Poncho in forum Software
    Replies: 7
    Last Post: 13-02-2004, 04:32 PM
  3. WinXP 2nd hd only in PIO mode
    By Lisandrea in forum PC Hardware and Components
    Replies: 10
    Last Post: 27-01-2004, 05:22 AM
  4. black dots in TV mode with herc 9800se aiw
    By Lisandrea in forum Graphics Cards
    Replies: 3
    Last Post: 13-01-2004, 12:56 PM
  5. Dammed PCI mode on AGP card
    By RoboPigeon in forum Graphics Cards
    Replies: 1
    Last Post: 29-12-2003, 12:45 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
  •