Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2

    Find multiple mode in array


    I am trying to find mode inside an array. By mode, I mean to find any numbers inside my array that is being repeated. The following code works but I do not want to use HashSet.

    Is there a way I could just use an array and loop through it to find the number of modes. I am only able to find a single mode without the use of a hashSet. Please advice. Thank you.

    Code that returns correctly but uses HashSet:

    Code:
    import java.util.HashSet;
    
    public class Mode {
    
        public static void main(String[] args) {
            int[] num = {2,3,4,7,7,7,8,8,8,8};   
            System.out.println(mode(num));
        }  
        
        public static HashSet<Integer> mode(int[] num){
            HashSet<Integer> list = new HashSet<>();
            for(int x=0; x < num.length -1; x++){
                for(int y=0; y < num.length; y++){
                    if(num[x] == num[y] && x != y){
                        list.add(num[y]); 
                    }
                }
            }
            return list;  
        }
    }
    Code that returns only one mode (The first mode found):

    Code:
    public class Mode {
    
        public static void main(String[] args) {
            int[] num = {2,3,4,5,8,8,8,7,7,7};
            int mode = mode(num);
            System.out.println(mode);
        }
    
        public static int mode(int[] num){
            for(int x=0; x < num.length; x++){
                for(int y=x+1; y < num.length; y++){
                    if(num[x] == num[y]){
                        return num[x]; 
                    }
                }
            }
            return num[0]; 
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Can the array's contents be modified as it is searched?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Hi,

    Need the array contents to remain the same. Was thinking if it would be sensible to have a separate array to collect the mode information.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    How would the modes be reported? Perhaps an array of the numbers.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Having problem with obtaining mutiple mode.

    In terms of reporting the mode, I just plan to loop and print it close to below:

    Code:
    for(int x : newArray){
                System.out.println(x);
            }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Sorry, I meant: how would the numbers that are repeats be saved so they could be returned to the caller. Your choice appears to be in an array.

    Now you need to work on the logic to find and save those numbers.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Sorry understood you wrongly in previous post.

    Not sure if I am moving further away from the answer.. Attempted to save the mode numbers using ArrayList as follows. Problem is I want the answer to be just 7,8 based on this array but I end up getting 7,7,8,8,8..

    Code:
    import java.util.ArrayList;
    
    public class Mode {
    
        public static void main(String[] args) {
            int[] num = {2,3,4,7,7,7,8,8,8,8};   
            ArrayList<Integer> list = new ArrayList<>();
            
            for(int x=0; x < num.length -1; x++){
                if(num[x] == num[x+1]){
                    list.add(num[x]); 
                }
            }
            
            System.out.println(list);
        }  
    }
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Before adding, check if it is already there.
    Do the repeated numbers need to be adjacent. Ie what about: 7,2,7?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Hi,

    Modified the code a little to do the check as follows:

    Code:
    if(!list.contains(num[x])){  
        list.add(num[x]); 
    }
    It is working now. As for the need to be adjacent, I am sorting the array via: Arrays.sort() prior to checking for mode.

    Do advice if I missed out anything or could have done it in a much shorter fashion. Thanks for guidance.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    I misunderstood what mode is. It is the single most common number in an array.

    My method works if the scenario is such that different numbers repeats the same number of times for example : [7,7,8,8] so it returns both 7 and 8. But if the list contains [7,7,8,8,8], I should only be returning 8 which I am not currently doing. Stuck again.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    With the contents of the array being sorted, it shouldn't be too hard. Start at the end and work backwards.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Sorry don't get it. Should I like add a counter for every possible repeated numbers followed by testing the counter value using if/else and see which is higher?

    Even then, I don't see how I could predict the number of counters needed.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    You're right, that won't help.

    predict the number of counters needed.
    It shouldn't be larger than the size of the array.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    I was trying to do as below where I test every possible index in the list and add counters to count and compare the frequency. I didn't do the entire testing cos believe it is going to quite a lengthy test. Fairly certain this is wrong way. Do advice. Thank you.

    Code:
    public static void main(String[] args) {  
            int[] numbers = {1,2,3,4,4,4,5,8,8,8,8}; 
            
            for(int x : mode(numbers)){
                System.out.print(x + " ");
            }
        }
        
        public static ArrayList<Integer> mode(int[] num){    
            ArrayList<Integer> list = new ArrayList<>();
            
            for(int x=0; x < num.length-1; x++){
                if(num[x] == num[x+1]){
                        list.add(num[x]); 
                } 
            }          
            
            int counter1 = 0;
            int counter2 = 0;
            int counter3 = 0;
            int counter4 = 0;
            int counter5 = 0;
    
            for(int x=0; x < list.size(); x++){
                if(list.get(0) == list.get(1)){
                    counter1++;
                }
                else if(list.get(0) == list.get(1) && list.get(1) == list.get(2)){
                    counter1++; 
                }
                else if(list.get(1) == list.get(2) && list.get(2) == list.get(3)){ 
                    counter2++; 
                }           
            }
            
            return list;
        }
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    what is the desired results?
    And why?

    The variables: counterN look like they should be an array.

    Why does the code use an ArrayList? Why not use the array directly?
    Last edited by NormR; November 29th, 2013 at 12:51 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo