Counting repeated elements in an integer array

前端 未结 13 1250
挽巷
挽巷 2020-11-27 08:02

I have an integer array crr_array and I want to count elements, which occur repeatedly. First, I read the size of the array and initialize it with numbers read

相关标签:
13条回答
  • 2020-11-27 08:18
    public class DuplicationNoInArray {
    
        /**
         * @param args
         *            the command line arguments
         */
        public static void main(String[] args) throws Exception {
            int[] arr = { 1, 2, 3, 4, 5, 1, 2, 8 };
            int[] result = new int[10];
            int counter = 0, count = 0;
            for (int i = 0; i < arr.length; i++) {
                boolean isDistinct = false;
                for (int j = 0; j < i; j++) {
                    if (arr[i] == arr[j]) {
                        isDistinct = true;
                        break;
                    }
                }
                if (!isDistinct) {
                    result[counter++] = arr[i];
                }
            }
            for (int i = 0; i < counter; i++) {
                count = 0;
                for (int j = 0; j < arr.length; j++) {
                    if (result[i] == arr[j]) {
                        count++;
                    }
    
                }
                System.out.println(result[i] + " = " + count);
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 08:23
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int[] numbers=new int[5];
        String x=null;
        System.out.print("enter the number 10:"+"/n");
        for(int i=0;i<5;i++){
            numbers[i] = input.nextInt();
        }
        System.out.print("Numbers  :  count"+"\n");
        int count=1;
        Arrays.sort(numbers);
        for(int z=0;z<5;z++){
            for(int j=0;j<z;j++){
                if(numbers[z]==numbers[j] & j!=z){
                    count=count+1;
                }
            }
            System.out.print(numbers[z]+" - "+count+"\n");
            count=1;
    
        }
    
    0 讨论(0)
  • 2020-11-27 08:25
    package jaa.stu.com.wordgame;
    
    /**
     * Created by AnandG on 3/14/2016.
     */
    public final class NumberMath {
        public static boolean isContainDistinct(int[] arr) {
    
            boolean isDistinct = true;
            for (int i = 0; i < arr.length; i++)
    
            {
    
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j] && i!=j) {
                        isDistinct = false;
                        break;
                    }
                }
    
            }
            return isDistinct;
        }
        public static boolean isContainDistinct(float[] arr) {
    
            boolean isDistinct = true;
            for (int i = 0; i < arr.length; i++)
    
            {
    
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j] && i!=j) {
                        isDistinct = false;
                        break;
                    }
                }
    
            }
            return isDistinct;
        }
        public static boolean isContainDistinct(char[] arr) {
    
            boolean isDistinct = true;
            for (int i = 0; i < arr.length; i++)
    
            {
    
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j] && i!=j) {
                        isDistinct = false;
                        break;
                    }
                }
    
            }
            return isDistinct;
        }
        public static boolean isContainDistinct(String[] arr) {
    
            boolean isDistinct = true;
            for (int i = 0; i < arr.length; i++)
    
            {
    
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j] && i!=j) {
                        isDistinct = false;
                        break;
                    }
                }
    
            }
            return isDistinct;
        }
        public static int[] NumberofRepeat(int[] arr) {
    
            int[] repCount= new int[arr.length];
            for (int i = 0; i < arr.length; i++)
    
            {
    
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j] ) {
                        repCount[i]+=1;
                    }
                }
    
            }
            return repCount;
        }
    }
    
    
    call  by NumberMath.isContainDistinct(array) for find is it contains repeat or not
    

    call by int[] repeat=NumberMath.NumberofRepeat(array) for find repeat count. Each location contains how many repeat corresponding value of array...

    0 讨论(0)
  • 2020-11-27 08:26
    for (int i = 0; i < x.length; i++) {
    
        for (int j = i + 1; j < x.length; j++) {
    
            if (x[i] == x[j]) {
                y[i] = x[i];
                times[i]++;
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 08:27
     public static void duplicatesInteger(int arr[]){
        Arrays.sort(arr);       
        int count=0;
        Set s=new HashSet();
        for(int i=0;i<=arr.length-1;i++){
            for(int j=i+1;j<=arr.length-1;j++){
                if(arr[i]==arr[j] && s.add(arr[i])){
                    count=count+1;              
                                    }
            }
            System.out.println(count);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 08:27
    public class FindRepeatedNumbers 
    {
     public static void main(String[] args) 
        {
         int num[]={1,3,2,4,1,2,4,6,7,5};
               Arrays.sort(num);
    
      for(int j=1;j<num.length;j++)
          {
           if(num[j]==num[j-1])
        {
                System.out.println(num[j]);
    
           }
       }
    
           }
         }
    
    0 讨论(0)
提交回复
热议问题