Count different values in array in Java

后端 未结 9 1634
借酒劲吻你
借酒劲吻你 2021-02-10 17:15

I\'m writing a code where I have an int[a] and the method should return the number of unique values. Example: {1} = 0 different values, {3,3,3} = 0 different values, {1,2} = 2 d

相关标签:
9条回答
  • 2021-02-10 17:36

    What about this?

    private <T> int arrayDistinctCount(T[] array) {
        return Arrays.stream(array).collect(Collectors.toSet()).size();
    }
    
    0 讨论(0)
  • 2021-02-10 17:41

    Try this:

    import java.util.ArrayList;
    public class DifferentValues {
    
     public static void main(String[] args)
      {
        int[] a ={1, 2, 3, 1};
        System.out.println(differentValuesUnsorted(a));
      }
    
     public static int differentValuesUnsorted(int[] a)
     {
       ArrayList<Integer> ArrUnique = new ArrayList<Integer>();
       int values=0;      //values of different numbers
       for (int num : a) {
           if (!ArrUnique.contains(num)) ArrUnique.add(num);
       }
       values = ArrUnique.size();
       if (values == 1) values = 0;       
       return values;
     }
    }
    

    input:{1,1,1,1,1} - output: 0
    input:{1,2,3,1} - output: 3

    0 讨论(0)
  • 2021-02-10 17:48

    Try this... its pretty simple using ArrayList. You don't even need two loop. Go on

    import java.util.*;
    public class distinctNumbers{
    
     public static void main(String []args){
        int [] numbers = {2, 7, 3, 2, 3, 7, 7};
        ArrayList<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<numbers.length;i++)
        {
    
            if(!list.contains(numbers[i]))  //checking if the number is present in the list
            {
                list.add(numbers[i]); //if not present then add the number to the list i.e adding the distinct number
            }
    
        }
        System.out.println(list.size());
    }
    }
    
    0 讨论(0)
  • 2021-02-10 17:49

    For small arrays, this is a fast concise method that does not require the allocation of any additional temporary objects:

    public static int uniqueValues(int[] ids) {
        int uniques = 0;
    
        top:
        for (int i = 0; i < ids.length; i++) {
            final int id = ids[i];
            for (int j = i + 1; j < ids.length; j++) {
                if (id == ids[j]) continue top;
            }
            uniques++;
        }
        return uniques;
    }
    
    0 讨论(0)
  • 2021-02-10 17:49

    This is actually much simpler than most people have made it out to be, this method works perfectly fine:

    public static int diffValues(int[] numArray){
        int numOfDifferentVals = 0;
    
        ArrayList<Integer> diffNum = new ArrayList<>();
    
        for(int i=0; i<numArray.length; i++){
            if(!diffNum.contains(numArray[i])){
                diffNum.add(numArray[i]);
            }
        }
    
        if(diffNum.size()==1){
                numOfDifferentVals = 0;
        }
        else{
              numOfDifferentVals = diffNum.size();
            } 
    
       return numOfDifferentVals;
    }
    

    Let me walk you through it:

    1) Provide an int array as a parameter.

    2) Create an ArrayList which will hold integers:

    • If that arrayList DOES NOT contain the integer with in the array provided as a parameter, then add that element in the array parameter to the array list
    • If that arrayList DOES contain that element from the int array parameter, then do nothing. (DO NOT ADD THAT VALUE TO THE ARRAY LIST)

    N.B: This means that the ArrayList contains all the numbers in the int[], and removes the repeated numbers.

    3) The size of the ArrayList (which is analogous to the length property of an array) will be the number of different values in the array provided.


    Trial

    Input:

      int[] numbers = {3,1,2,2,2,5,2,1,9,7};
    

    Output: 6

    0 讨论(0)
  • 2021-02-10 17:54

    You can use a HashSet, which can only contain unique elements. The HashSet will remove the duplicated items and you can then get the size of the set.

    public static int differentValuesUnsorted(int[] a) {
        Set<Integer> unique = new HashSet<Integer>();
        for (int val : a) {
            unique.add(val); // will only be added if a is not in unique
        }
        if (unique.size() < 2) { // if there are no different values
            return 0;
        }
        return unique.size();
    }
    
    0 讨论(0)
提交回复
热议问题