是否有可能在arraylist中找出值是否存在两次?

时间:2013-01-06 00:14:17

标签: java arraylist

我有一个整数arraylist ..

ArrayList <Integer> portList = new ArrayList();

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

8 个答案:

答案 0 :(得分:11)

您可以使用类似的内容来查看特定值的次数:

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

答案 1 :(得分: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
}

答案 2 :(得分:3)

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

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
}

答案 3 :(得分:3)

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

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

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

    return numCount > 1;
}

答案 4 :(得分:2)

Set portSet = new HashSet<Integer>();
portSet.addAll(portList);
boolean listContainsDuplicates = portSet.size() != portList.size();

答案 5 :(得分:0)

我使用以下解决方案找出ArrayList是否包含多个数字。此解决方案与上面user3690146列出的解决方案非常接近,但根本不使用辅助变量。运行后,你得到&#34;这个号码被列出不止一次&#34;作为回复信息。

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;
    }

}

答案 6 :(得分: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
    }

答案 7 :(得分:-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;
}