Reyjohn
Reyjohn

Reputation: 2733

how to get the most common character in an array?

Suppose I have an integer array like this: {5,3,5,4,2}

and I have a method which returns the most common character

public int highestnumber(String[] num) {

        int current_number = Integer.parseInt(num[0]);
        int counter = 0;
        for (int i = 1; i < num.length; ++i) {
            if (current_number == Integer.parseInt(num[i])) {
                ++counter;

            } else if (counter == 0) {
                current_number = Integer.parseInt(num[i]);
                ++counter;

            } else {
                --counter;

            }
        }

        return current_number;
    }

but if I have multiple common character then i need to get the number which is closest to one(1), like if i have an array like this: {5,5,4,4,2};

then the method should return 4, what should I do for this?

Upvotes: 2

Views: 4202

Answers (4)

Shahjahan Ahmed Sajib
Shahjahan Ahmed Sajib

Reputation: 21

You can also try this:

import java.util.TreeMap;

public class SmallestFrequentNumberFinder {

    public static int[] stringToIntegerArray(String[] stringArray) {
        int[] integerArray = new int[stringArray.length];
        for (int i = 0; i < stringArray.length; i++) {
            integerArray[i] = Integer.parseInt(stringArray[i]);
        }
        return integerArray;
    }

    public static int getSmallestFrequentNumber(int[] numbers) {
        int max = -1;
        Integer smallestFrequentNumber = null;
        TreeMap<Integer, Integer> frequencyMaper = new TreeMap<Integer, Integer>();

        for (int number : numbers) {
            Integer frequency = frequencyMaper.get(number);
            frequencyMaper.put(number, (frequency == null) ? 1 : frequency + 1);
        }

        for (int number : frequencyMaper.keySet()) {
            Integer frequency = frequencyMaper.get(number);
            if (frequency != null && frequency > max) {
                max = frequency;
                smallestFrequentNumber = number;
            }
        }
        return smallestFrequentNumber;
    }

    public static void main(String args[]) {
        String[] numbersAsString = {"5", "5", "4", "2", "4", "4", "2", "2"};
        final int[] integerArray = stringToIntegerArray(numbersAsString);
        System.out.println(getSmallestFrequentNumber(integerArray));
    }
}

Upvotes: 1

UlyssesSKrunk
UlyssesSKrunk

Reputation: 11

Fast way. Create a counter int array one element for each number. Go through the array once and increment corresponding counter array for each number. Set highest number to first counter element then go through and change highest number to current element only if it is bigger than highest number, return highest number.

public int highestNumber(String[] num){
    int[] count = new int[10];
    int highest_number = 0;
    int highest_value = 0;

    for(int i = 0; i < num.length; i++)
        count[Integer.parseInt(num[i])]++;;

    for(int i = 0; i < count.length; i++)
        if(count[i] > highest_value){
            highest_number = i;
            highest_value = count[i];
        }

    return highest_number;
}

10x slower but without other array. Create three ints one for number and two for counting. Go through the array once for each int and increment current counting each time it shows up, if bigger that highest count, set to highest count and set highest number to current count. Return highest number.

public int highestNumber(String[] num){
    int highest_number = 0;
    int highest_value = 0;
    int current_value = 0;

    for(int i = 0; i < 10; i++){
        for(int j = 0; j < num.length; j++)
            if(i == Integer.parseInt(num[j]))
                current_value++;

        if(current_value > highest_value){
            highest_value = current_value;
            highest_number = i;
        }

        current_value = 0;
    }

    return highest_number;
}

The first is obviously much faster but if for whatever reason you don't want another array the second one works too.

Upvotes: 1

user370305
user370305

Reputation: 109257

As per what I understand your question,

What you have to done is,

1. Create ArrayList from your int[]
2. Use HashMap for find duplicates, which one is unique
3. Sort it as Ascending order, 
4. First element is what you want..

EDIT: Answer for your question

int[] arr = {5, 4, 5, 4, 2};
ArrayList<Integer> resultArray = new ArrayList<Integer>();
Set<Integer> set = new HashSet<Integer>();

for (int i = 0; i < arr.length; i++)
    {
     if (set.contains(arr[i]))
    {
     System.out.println("Duplicate value found at index: " + i);
     System.out.println("Duplicate value: " + arr[i]);
         resultArray.add(arr[i]);
    }
    else
    {
    set.add(arr[i]);
    }
   }
Collections.sort(resultArray);

for (int i = 0; i < resultArray.size(); i++)
{
Log.e("Duplicate Values:", resultArray.get(i) + "");
}

Your need is,

int values = resultArray.get(0);

Upvotes: 2

Michael Burr
Michael Burr

Reputation: 340436

Sort the array then count runs of values.

Upvotes: 2

Related Questions