Basic Java Programs - Part 1


1 . Count occurrences of elements of list in Java

import java.util.HashMap;

import java.util.TreeMap;

 

public class CountOccurrences {

 

    public static void main(String[] args) {

        String str[] = {"Rahul", "Trupti", "Rahul", "Yatin", "Keyur", "Yatin", "Manoj", "Yatin", "Trupti"};

        countOccurrencesName(str);

    }

 

    private static void countOccurrencesName(String[] str) {

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

        for (int i = 0; i < str.length; i++) {

            String ch = str[i];

            if (map.get(ch) != null) {

                map.put(ch, map.get(ch) + 1);

            } else {

                map.put(ch, 1);

            }

        }

        TreeMap<String, Integer> t = new TreeMap<>(map);

        System.out.println(t);

    }

}

Output :

{Keyur=1, Manoj=1, Rahul=2, Trupti=2, Yatin=3}

2.Count Occurrences of a Char in a String

import java.util.HashMap;

 

public class CountOccurrencesChar {

 

    public static void main(String[] args) {

        String ch = "Chaudhari Rahul";

        char[] ch1 = ch.toCharArray();

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

        for (int i = 0; i < ch1.length - 1; i++) {

            char c = ch1[i];

 

            if (map.containsKey(c)) {

                map.put(c, map.get(c) + 1);

            } else {

                map.put(c, 1);

            }

        }

        System.out.println(map);

    }

}

Output :

{ =1, a=3, r=1, R=1, C=1, d=1, u=2, h=3, i=1}


3.Find the element which appears maximum number of times in the array.

import java.util.ArrayList;

import java.util.Collections;

 

public class RepeatedMaxNo {

 

    public static void main(String[] args) {

        int num[] = {99, 77, 07, 99, 007, 99, 01, 66, 99};

        int result = countRepeateNo(num);

        System.out.println("Max Repeated Number : " + result);

    }

 

    private static int countRepeateNo(int[] num) {

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

        int count = 0;

        int max = 0;

        String m = "";

        int ii = 0;

        boolean flag = false;

        for (int i = 0; i < num.length; i++) {

            for (int j = i + 1; j < num.length - 1; j++) {

                if (num[i] == num[j]) {

                    count++;

                    flag = true;

                }

            }

            if (flag == true) {

                list.add(count + 1);

                count = 0;

                max = (Integer) Collections.max(list);

            }

        }

        return max;

    }

}

Output :

Max Repeated Number : 3

One More

public class MaxRepeatingBruteForce {

 

    public void MaxRepeatingElement(int[] arrA) {

        int maxCounter = 0;

        int element = 0;

        for (int i = 0; i < arrA.length; i++) {

            int counter = 1;

            for (int j = i + 1; j < arrA.length; j++) {

                if (arrA[i] == arrA[j]) {

                    counter++;

                }

            }

            if (maxCounter < counter) {

                maxCounter = counter;

                element = arrA[i];

            }

        }

        System.out.println("Element repeating maximum no of times: " + element + ", maximum count: " + maxCounter);

    }

 

    public static void main(String[] args) {

        int[] arrA = {4, 1, 5, 2, 1, 5, 9, 8, 6, 5, 3, 2, 4, 7};

        MaxRepeatingBruteForce m = new MaxRepeatingBruteForce();

        m.MaxRepeatingElement(arrA);

    }

}

Output :

Element repeating maximum no of times: 5, maximum count: 3

Post a Comment

0 Comments