Java 如何从数组中获取最常见的met字符(泛型)

Java 如何从数组中获取最常见的met字符(泛型),java,Java,假设(新整数[]{10,10,20,10,20,30,10,20,30,40,50,10,20,30,40,40,50,60})被传递到方法公共静态对模式(T项[])。该方法应该返回10,5(数组中有五个数字10)。S,基本上该方法返回数组中最频繁的元素。我应该如何完成这个方法?请不要使用映射,集合,哈希集,只允许使用数组列表 import java.util.ArrayList; public class Mode { public static <T> P

假设
(新整数[]{10,10,20,10,20,30,10,20,30,40,50,10,20,30,40,40,50,60})
被传递到方法
公共静态对模式(T项[])
。该方法应该返回
10,5
(数组中有五个数字10)。S,基本上该方法返回数组中最频繁的元素。我应该如何完成这个方法?请不要使用
映射
集合
哈希集
,只允许使用
数组列表

      import java.util.ArrayList;

public class Mode {

    public static <T> Pair<T, Integer> mode(T items[])
    {
        ArrayList <Pair<T, Integer>> temp = new ArrayList<>();
        //ArrayList <T> temp = new ArrayList<>();

        for(T values: items)
        {
            temp.add((Pair<T, Integer>) values);
        }

        for(int i = 0; i < temp.size(); i++)
            for(int j = 0; j < temp.size(); j++)
        {
            if(temp.get(i) == temp.get(j))
        }
    }
}


    public class Pair<X,Y>{
      private X first;
      private Y second;

      public Pair(X x, Y y){
        this.first = x;
        this.second = y;
      }

      public X getFirst(){
        return this.first;
      }
      public Y getSecond(){
        return this.second;
      }

      public boolean equals(Object o){
        if(!(o instanceof Pair)){
          return false;
        }
        Pair p = (Pair) o;
        return
          this.first.equals(p.first) &&
          this.second.equals(p.second);
      }

      public String toString(){
        return String.format("(%s,%s)",first,second);
      }

    }
import java.util.ArrayList;
公共课模式{
公共静态对模式(T项[])
{
ArrayList temp=新的ArrayList();
//ArrayList temp=新的ArrayList();
对于(T值:项目)
{
临时添加((对)值);
}
对于(int i=0;i
迈克,请尝试以下操作:

import java.util.ArrayList;
public class Mode {

public static Pair<T, Integer> mode(T[] a)
 {

    int count = 1, tempCount;
    T popular = a[0];
    T temp;
    for (int i = 0; i < (a.length - 1); i++)
    {
        temp = a[i];
        tempCount = 1;
        for (int j = 1; j < a.length; j++)
        {
          if (temp.equals(a[j]))
            tempCount++;
        }
        if (tempCount > count)
        {
          popular = temp;
          count = tempCount;
        }
      }
      return new Pair(popular, new Integer(count));
        }
    }


public class Pair<X,Y>{
  private X first;
  private Y second;

  public Pair(X x, Y y){
    this.first = x;
    this.second = y;
  }

  public X getFirst(){
    return this.first;
  }
  public Y getSecond(){
    return this.second;
  }

  public boolean equals(Object o){
    if(!(o instanceof Pair)){
      return false;
    }
    Pair p = (Pair) o;
    return
      this.first.equals(p.first) &&
      this.second.equals(p.second);
  }

  public String toString(){
    return String.format("(%s,%s)",first,second);
  }

}
import java.util.ArrayList;
公共课模式{
公共静态对模式(T[]a)
{
int count=1,tempCount;
T popular=a[0];
温度;
对于(int i=0;i<(a.length-1);i++)
{
温度=a[i];
tempCount=1;
对于(int j=1;j计数)
{
流行=临时;
计数=临时计数;
}
}
返回新对(常用,新整数(计数));
}
}
公共类对{
私人X优先;
二等兵;
公共对(X,Y,Y){
第一个=x;
这个秒=y;
}
公共X getFirst(){
先把这个还给我;
}
公共Y getSecond(){
把这个还给我;
}
公共布尔等于(对象o){
如果(!(对的o实例)){
返回false;
}
对p=(对)o;
返回
这个。第一。等于(p。第一)&&
这个秒等于(p秒);
}
公共字符串toString(){
返回String.format((%s,%s)”,第一个,第二个);
}
}

如果您有任何问题,请告诉我。

如果打成平局会发生什么?这会产生一对
10,4
而不是
10,5
,例如OP发布的示例。同时
T温度=0
????进一步的改进建议:只计算元素之后出现的元素(这足以使数组中第一次出现元素时的计数正确)。fabian,谢谢你的建议,非常好!对前两个进行了更改;并将考虑改进建议!