Android (method for checking an arrays for repeated numbers?)

前端 未结 3 1842
时光取名叫无心
时光取名叫无心 2021-01-28 23:40

could any one help me. i am making an app, and in the java, numbers are send to a int array and i need to check if any of the numbers in the array repeated and if there are to c

3条回答
  •  清酒与你
    2021-01-29 00:21

    You can use Collections.frequency() method to get count of how many times a number is repeated.Depending on what you want, you can iterate over an array of Integers and check how many times each one repeated. if an items frequency is greater than 1 than it is repeating in that array. You can adapt the following code according to your needs. Note: by the way System.out.println() gives output to eclipse log cat section. it is just for demonstration.

    ArrayList nums = new ArrayList();
            nums.add(new Integer(3));
            nums.add(new Integer(3));
            nums.add(new Integer(3));
            nums.add(new Integer(2));
            nums.add(new Integer(2));
            nums.add(new Integer(1));
            System.out.println("Number of 1's" + " " + Collections.frequency(nums, 1));
            System.out.println("Number of 2's" + " " + Collections.frequency(nums, 2));
            System.out.println("Number of 3's" + " " + Collections.frequency(nums, 3));
    

提交回复
热议问题