Saturday, 15 August 2020

No of Occurrence in Array

package com.tutorial4u;

import java.util.HashMap;

/************************************************************

 No of Occurrence in Array

************************************************************/

public class NoOfOccurrenceInArray {

      public static void main(String[] args) {

            int [] numbers = new int[] {10,12,13,14,12,11,10,20,21,14,22};

            int count =0;

            HashMap<Integer, Integer> map = new HashMap<>();

            for(Integer i : numbers) {

                  if(map.containsKey(i)) {

                        count = map.get(i);

                        map.put(i, count+1);

                  }else {

                        map.put(i, 1);

                  }

            }

            System.out.println(map);

      }

}

 

 Output :

{20=1, 21=1, 22=1, 10=2, 11=1, 12=2, 13=1, 14=2}

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...