Counting repeated elements in an integer array

前端 未结 13 1325
挽巷
挽巷 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: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...

提交回复
热议问题