9

我有一个整数数组列表..

ArrayList <Integer> portList = new ArrayList();

我需要检查一个特定的整数是否已经输入了两次。这在Java中可能吗?

4

8 回答 8

18

您可以使用类似这样的方法来查看特定值存在多少次:

System.out.println(Collections.frequency(portList, 1));
// there can be whatever Integer, i put 1 so you can understand

并且要检查一个特定的值是否不止一次,你可以使用这样的东西:

if ( (Collections.frequency(portList, x)) > 1 ){
    System.out.println(x + " is in portList more than once ");
} 
于 2013-01-06T00:27:49.410 回答
6

我知道这是一个老问题,但由于我在这里寻找答案,我想我会分享我的解决方案

public static boolean moreThanOnce(ArrayList<Integer> list, int searched) 
{
    int numCount = 0;

    for (int thisNum : list) {
        if (thisNum == searched) numCount++;
    }

    return numCount > 1;
}
于 2014-05-30T06:04:47.917 回答
4

这将告诉您您的数据中是否至少有两个相同的值ArrayList

int first = portList.indexOf(someIntValue);
int last  = portList.lastIndexOf(someIntValue);
if (first != -1 && first != last) {
    // someIntValue exists more than once in the list (not sure how many times though)
}

**编辑**

如果您真的想知道给定值有多少重复项,则需要遍历整个数组。像这样的东西:

/**
 * Will return a list of all indexes where the given value
 * exists in the given array. The list will be empty if the
 * given value does not exist at all.
 *
 * @param List<E> list
 * @param E value
 * @return List<Integer>    a list of indexes in the list
 */
public <E> List<Integer> collectFrequency(List<E> list, E value) {
   ArrayList<Integer> freqIndex = new ArrayList<Integer>();
   E item;
   for (int i=0, len=list.size(); i<len; i++) {
       item = list.get(i);
       if ((item == value) || (null != item && item.equals(value))) {
           freqIndex.add(i);
       }
   }
   return freqIndex;
}

if (!collectFrequency(portList, someIntValue).size() > 1) {
    // duplicate value
}

或者干脆

if (Collections.frequency(portList, someIntValue) > 1) {
    // duplicate value
}
于 2013-01-06T00:17:37.370 回答
4

如果你想用一种方法做到这一点,那么不。但是,如果您需要简单地找出它在列表中至少存在一次,您可以分两步进行。你可以做

int first = list.indexOf(object)
int second = list.lastIndexOf(object) 
//Don't forget to also check to see if either are -1, the value does not exist at all.
if (first == second) {
    // No Duplicates of object appear in the list
} else {
    // Duplicate exists
}
于 2013-01-06T00:18:47.997 回答
2
Set portSet = new HashSet<Integer>();
portSet.addAll(portList);
boolean listContainsDuplicates = portSet.size() != portList.size();
于 2013-01-06T00:19:03.133 回答
1

我使用以下解决方案来确定 ArrayList 是否包含多个数字。该解决方案非常接近user3690146上面列出的解决方案,但根本不使用辅助变量。运行后,您会收到“该号码被多次列出”作为返回消息。

public class Application {

    public static void main(String[] args) {

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

        list.add(4);
        list.add(8);
        list.add(1);
        list.add(8);

        int number = 8;

        if (NumberMoreThenOnceInArray(list, number)) {
            System.out.println("The number is listed more than once");
        } else {
            System.out.println("The number is not listed more than once");
        }
    }

    public static boolean NumberMoreThenOnceInArray(ArrayList<Integer> list, int whichNumber) {

        int numberCounter = 0;

        for (int number : list) {
            if (number == whichNumber) {
                numberCounter++;
            }
        }

        if (numberCounter > 1) {
            return true;
        }

        return false;
    }

}
于 2015-07-07T18:23:51.893 回答
0

这是我的解决方案(在 Kotlin 中)

    // getItemsMoreThan(list, 2) -> [4.45, 333.45, 1.1, 4.45, 333.45, 2.05, 4.45, 333.45, 2.05, 4.45] -> {4.45=4, 333.45=3}
    // getItemsMoreThan(list, 1)->  [4.45, 333.45, 1.1, 4.45, 333.45, 2.05, 4.45, 333.45, 2.05, 4.45] -> {4.45=4, 333.45=3, 2.05=2}
    fun getItemsMoreThan(list: List<Any>, moreThan: Int): Map<Any, Int> {
        val mapNumbersByElement: Map<Any, Int> = getHowOftenItemsInList(list)
        val findItem = mapNumbersByElement.filter { it.value > moreThan }
        return findItem
    }

    // Return(map) how often an items is list.
    // E.g.: [16.44, 200.00, 200.00, 33.33, 200.00, 0.00] -> {16.44=1, 200.00=3, 33.33=1, 0.00=1}
    fun getHowOftenItemsInList(list: List<Any>): Map<Any, Int> {
        val mapNumbersByItem = list.groupingBy { it }.eachCount()
        return mapNumbersByItem
    }
于 2020-01-11T13:21:08.543 回答
-2

通过查看问题,我们需要找出一个值是否在 ArrayList 中存在两次。所以我相信我们可以通过下面的简单检查来减少“遍历整个列表只是为了检查值是否只存在两次”的开销。

public boolean moreThanOneMatch(int number, ArrayList<Integer> list) {

    int count = 0;
    for (int num : list) {
        if (num == number) {
            count ++ ;
            if (count == 2) {
                return true;
            }
        }
    }
    return false;
}
于 2016-08-29T14:09:00.727 回答