Find the mode of the numbers in a array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Find the mode of the numbers in a array

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    1

    Find the mode of the numbers in a array

    Hi there,
    I need to write a java code to find the mode of a set of numbers (number i.e., repeated most number of times in a array). Any ideas/help is appreciated......

    Thanks in advance....

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Code:
    sort the array
    int max = 0;
    int count = 0;
    int sourceIndex = 0;
    int maxIndex = 0;
    int currentIndex = 0;
    Type maxElement = null;
    for( ; currentIndex < arraylength; ++currentIndex ) 
         sourceIndex = currentIndex;
         sourceElement = array[currentIndex];
         while array[currentIndex] equals sourceElement
            count++;
            currentIndex++;
         if count is larger than max 
            max = count;
            maxIndex = sourceIndex;
            maxElement = sourceElement;
         count = 0;
    return sourceIndex
    Return sourceIndex if you want to know where the first occurrence of the element is, or sourceElement if you want to know the "value" of the element
    Last edited by nspils; 02-27-2007 at 09:21 AM.

Similar Threads

  1. Replies: 4
    Last Post: 11-08-2006, 04:19 PM
  2. create an array of unique numbers vb.net
    By wartech in forum .NET
    Replies: 4
    Last Post: 08-15-2006, 12:01 AM
  3. Index Outside the Bounds of the Array
    By Jason Salas in forum ASP.NET
    Replies: 2
    Last Post: 04-24-2003, 09:25 PM
  4. Replies: 0
    Last Post: 01-27-2003, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center