Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取数组中最常见的字符?_Java - Fatal编程技术网

Java 如何获取数组中最常见的字符?

Java 如何获取数组中最常见的字符?,java,Java,假设我有一个整数数组,如下所示: {5,3,5,4,2} 我有一个方法,返回最常见的字符 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 ==

假设我有一个整数数组,如下所示: {5,3,5,4,2}

我有一个方法,返回最常见的字符

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;
    }
public int highestnumber(字符串[]num){
int current_number=Integer.parseInt(num[0]);
int计数器=0;
对于(int i=1;i
但是如果我有多个公共字符,那么我需要得到最接近一(1)的数字,就像我有这样一个数组: {5,5,4,4,2};


那么方法应该返回4,我应该为此做些什么?

根据我对您问题的理解

你要做的是

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..
编辑:回答您的问题

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) + "");
}

对数组排序,然后计算值的运行次数。

快速方式。 为每个数字创建一个计数器int数组一个元素。遍历数组一次,并为每个数字增加相应的计数器数组。将最大值设置为第一个计数器元素,然后进行检查,只有当最大值大于最大值时,才将最大值更改为当前元素,返回最大值

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;
}
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;
}
public int highestNumber(字符串[]num){
int[]计数=新的int[10];
int最高_数=0;
int最高_值=0;
for(int i=0;i最高值){
最高_数=i;
最高_值=计数[i];
}
返回最高的_数;
}
速度慢10倍,但没有其他阵列。 创建三个整数,一个用于数字,两个用于计数。为每个int遍历数组一次,并在每次显示时增加当前计数,如果最大计数较大,则设置为最大计数,并将最大数字设置为当前计数。返回最大值

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;
}
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;
}
public int highestNumber(字符串[]num){
int最高_数=0;
int最高_值=0;
int当前_值=0;
对于(int i=0;i<10;i++){
对于(int j=0;j最高_值){
最高_值=当前_值;
最高_数=i;
}
当前_值=0;
}
返回最高的_数;
}
第一个显然要快得多,但如果出于任何原因您不需要另一个阵列,第二个阵列也可以工作。

您也可以尝试以下方法:

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));
    }
}
import java.util.TreeMap;
公共类SmallestFrequentNumberFinder{
公共静态int[]StringToIntegerary(字符串[]stringArray){
int[]integerArray=新的int[stringArray.length];
对于(int i=0;imax){
最大值=频率;
smallestFrequentNumber=编号;
}
}
返回最小频数;
}
公共静态void main(字符串参数[]){
字符串[]numbersaString={“5”、“5”、“4”、“2”、“4”、“4”、“2”、“2”};
final int[]integerArray=stringToIntegerArray(numbersastring);
System.out.println(getSmallestFrequentNumber(integerArray));
}
}

如果你的数组是这样的{5,4,5,5,4,2,2,5},那么你想要哪个数字?还有一个问题,为什么要传递字符串数组?它可能是整数数组否?你想要普通字符还是普通数字?在这种情况下,我得到5,这是正确的,但只有当最普通的字符是多个时,我才面临这个问题。比如,如果我在一个数组中有5个三次和4个三次,那么我应该得到4,因为它最接近1。对于我的编码,我必须通过字符串数组,这不是一个大交易,这与android无关。添加代码的上下文并不总是相关的。请看我编辑的答案,你问题的完整答案:-)我在你的帖子中没有回答,请检查我认为你没有正确理解我的问题。我从我自己的方法中得到了最常见的字符,我同意。但当答案是多个时,我面临的问题是,如果我有两个数字同时出现在该数组中,那么它应该返回最小的一个。在ArrayList中添加方法中所有最常见的字符,然后按升序排序。我正在尝试,我会让你知道的?看看我编辑的答案,你的问题的完整答案:-)好的,我正在尝试:)谢谢你的努力