Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bijay-shrestha/216c7083ee45ba4e64a0bfa9be09ce28 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/216c7083ee45ba4e64a0bfa9be09ce28 to your computer and use it in GitHub Desktop.
package com.basic.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HighestOccurringDigitInNumber {
public static void main(String[] args) {
// int number = 1223412;
int number = 122341112;
log.info("Finding highest occurring digit in a number {}.", number);
log.info("Actual Result: {}", findHighestOccurringDigit(number));
}
static int findHighestOccurringDigit(int n) {
int result = 0;
int count = 0;
int maxCount = 1;
for (int i = 0; i <= 9; i++) {
count = countOccurrence(n, i);
if (count >= maxCount) {
maxCount = count;
result = i;
}
}
return result;
}
static int countOccurrence(int n, int i) {
int count = 0;
while (n > 0) {
if (n % 10 == i) {
count++;
}
n = n / 10;
}
return count;
}
}
@coder-shankar
Copy link

    static int findHighestOccurringDigit(int n) {
        if (n >= 0 && n <= 9) {
            return n;
        }
        int[] a = new int[10];
        while (n > 0) {
            int digit = n % 10;
            a[digit] += 1;
            n = n / 10;
        }

        int maxIndex = 0;
        for (int i = 1; i < a.length; i++) {
            if (a[i] > a[maxIndex]) {
                maxIndex = i;
            }
        }

        return maxIndex;
    }

@sanjmgr
Copy link

sanjmgr commented Aug 19, 2022

package problems;

public class MostFrequentNumber {
    public static void main(String[] args) {
        int number = 1223412122;
        String str = "1223412122444444";
        mostFrequentNumber(number);
        mostFrequentString(str);
    }

    private static int mostFrequentString(String str) {
        int[] frequency = new int[10];
        for (int i = 0; i < str.length(); i++) {
            int digit = str.charAt(i) - '0';
            frequency[digit]++;
        }

        int maxFrequent = 0;
        int index = 0;
        for (int i = 0; i < frequency.length; i++) {
            if (frequency[i] > maxFrequent) {
                maxFrequent = frequency[i];
                index = i;
            }
        }

        System.out.println("max frequent string is " + index + " with frequency of " + maxFrequent);

        return index;
    }

    private static int mostFrequentNumber(int number) {
        int[] frequency = new int[10];
        while (number > 0) {
            int digit = number % 10;
            number /= 10;

            frequency[digit]++;
        }

        int index = 0;
        int maxFrequency = 0;
        for (int i = 0; i < frequency.length; i++) {
            if (frequency[i] > maxFrequency) {
                maxFrequency = frequency[i];
                index = i;
            }
        }

        System.out.println("max frequent number is " + index + " with frequency of " + maxFrequency);

        return index;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment