Java program to find the occurence of integer in a given integer array.


package com.javatechtube;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IntegerOccurence {

  public static void main(String[] args) {

 int[] arr = {1,2,3,2,4,5};

 List<Integer> list = new ArrayList<>();

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

 for(int i=0; i<arr.length;i++)
        {
    if(map.containsKey(arr[i]))
           {
  Integer value = (Integer)map.get(arr[i]);

  map.put(arr[i],++value);
    }
           else
           {
                int key = arr[i];

  map.put(key, 1);

  list.add(arr[i]);
    }
  }
         System.out.println(map);
     }
}
Share:

1 comment:

Quote of the day

Popular Posts

Featured Post

Collection Framework Overview

Collection:- A collection (sometimes called a container) is simply an object that groups multiple elements into single unit. Collect...

Youtube Page

Facebook Page

Recent Posts

About

I am Shivaji Chandra and I'm a Computer Science Engineer. I love to write poems, jokes and quotes. click here →