Counting an Occurrence in an Array (Java)

后端 未结 12 1736
情歌与酒
情歌与酒 2020-12-16 05:23

I am completely stumped. I took a break for a few hours and I can\'t seem to figure this one out. It\'s upsetting!

I know that I need to check the current element in

相关标签:
12条回答
  • 2020-12-16 05:59

    You need two loops:

    1. For where you're starting

    2. A nested loop, to be one index in front of where you're currently at, unless you're at the end.

    Is there a number you don't EVERY expect to be in your array? If so, use that value (-1 for example) as a sentinel value to overwrite your occurrences as they are counted. Then as you go through the array again for the next number to check for occurrences, you skip it if it has your sentinel value.

    0 讨论(0)
  • 2020-12-16 05:59

    You can find the answer of your question here

    I used Arrays.sort() method in my example:

    public class MyTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            int[] a = {1, 9, 8, 8, 7, 6, 5, 4, 3, 3, 2, 1};
    
            Arrays.sort(a);
            int nbOccurences = 0;
    
            for (int i = 0, length = a.length - 1; i < length; i++) {
                if (a[i] == a[i + 1]) {
                    nbOccurences++;
                }
            }
    
            System.out.println("Number same occurences : " + nbOccurences);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 05:59

    Just copy and execute it, it will give you the no of occurrence of integers in array.

    public class noOfOccurence{  
    
    public static void main(String[] args){
    
        int a[] = {1,9,4,5,6,7,5,6,7,3,2,5,7,9,0,4,3,5,1,4,6,0,2,3,1,4,3,8};
    
        HashSet<Integer> al = new HashSet<Integer>();
    
       //Store the array in set as set will store unique elemnets
        for(int i=0;i<a.length;i++){
            //int count =0; 
            al.add(a[i]);
        }
        //printing the set
        System.out.println("al "+al);
    
    
        for(int set : al){
            int count = 0;
            for(int j=0;j<a.length;j++){
    
                if(set==a[j]){
                    count++;
                }
            }
            System.out.println(set+" occurs "+count+" times");
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-16 06:03
    // i use List<Integer> to solve the problem. it's not a concise way
    
    public static List<List<Integer>> occurence(int[] cards) {
    
      // first, we create a ArrayList to store the distinct number and its corresponding count value
      //it takes time
      List<List<Integer>> element = new ArrayList<>();
    
      int tmp=cards[0],  count=0;
      int total = cards.length;
    
      for(int i=0; i < total; i++) {
    
        if(i == total -1) {
          if( cards[i] == tmp) {
    
              List<Integer> l = new ArrayList<>();
              l.add(tmp);
              l.add(count+1);
              element.add(l);
              break;
          }else {
            List<Integer> l = new ArrayList<>();
            l.add(tmp);
            l.add(count);
            element.add(l);
    
            l = new ArrayList<>();
            l.add(cards[i]);
            l.add(1);
            element.add(l);
            break;
          }
    
        }
    
        if(cards[i] == tmp) {
          count++;        
        }else { 
          List<Integer> l = new ArrayList<>();
          l.add(tmp);
          l.add(count);
          element.add(l);
    
          tmp = cards[i];
          count = 1;  //we already have 1 occurence of cards[i]. i.e. tmp       
        }
      }
    
      return element;
    }
    
    0 讨论(0)
  • 2020-12-16 06:08

    The most efficient way is to create hashmap to save the occurrence of element while iterating the array. It will do it in 2n time complexity which is best for this problem -

    HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
    int count;    
    for(int i=0;i<arr.length;i++){
           if(hmap.get(arr[i])==null){
             hmap.put(arr[i],1);
           }else{
             count=hmap.get(arr[i]);
             count++;
             hmap.put(arr[i],count);
           }
         }
    
    0 讨论(0)
  • 2020-12-16 06:08
    int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};  
    
    //Array fr will store frequencies of element  
    int [] fr = new int [arr.length];  
    int visited = -1;  
    for(int i = 0; i < arr.length; i++){  
        int count = 1;  
        for(int j = i+1; j < arr.length; j++){  
            if(arr[i] == arr[j]){  
                count++;  
                //To avoid counting same element again  
                fr[j] = visited;  
            }  
        }  
        if(fr[i] != visited)  
            fr[i] = count;  
    }
    
    0 讨论(0)
提交回复
热议问题