smali

среда, 31 декабря 2014 г.

java calculate number of duplicates in array of integer numbers


package myjavaprogram;
import java.util.Arrays;

public class TestClass1 {
    public static void main(String[] args){
        int[] array = {23, 23, 0, 43, 545, 12, -55, 43, 12, 12, -999, -87, 12, 0, 0};
        //int[] array = {23, -22, 0, 43, 545, 12, -55, 43, 12, 0, -999, -87, 12};
        //int[] array = {23, -22, 0, 23};
        //int[] array = {23, -22, 23};
        calculate_duplicates(array);        
    }
    
    private static void calculate_duplicates(int[] array) {
        calculateUniqueNumbers(array);
    }

    private static void calculateUniqueNumbers(int[] array) {
        Pair[] pairs = new Pair[array.length];
        initializePairsAtrray(pairs, array);
        printPairsAtrray(pairs);
        
        System.out.println("array.length="+array.length);
        System.out.println("--------------------");
        
        // update pairs array taking in account duplicates duplicates
        for(int i = 0; i < array.length; i++) {
            System.out.println("array[i]="+array[i] + " i="+i);
            for(int j = i+1; j < array.length; j++) {
                System.out.println("array[j]="+array[j]+" j="+j);

                if(array[i] == array[j] && pairs[j].useDuringCount == true) {
                    pairs[i].occurance_num++;
                    
                    pairs[j].occurance_num = 0;
                    pairs[j].useDuringCount = false;
                }
                
                if(array[i] == 0) {
                    pairs[i].occurance_num = 0;                    
                }
                if(array[j] == 0) {
                    pairs[j].occurance_num = 0;
                    pairs[j].useDuringCount = false;
                }                
            }
            pairs[i].useDuringCount = false;
            System.out.println("--------------------");
        }                
        printPairsAtrray(pairs);
        
        // calculate general number of duplicates (numbers whick are repeated 
        // in initial array)
        System.out.println("Duplicates in array:"+
                calculateDuplicatesNumber(pairs));        
    }

    private static void initializePairsAtrray(Pair[] pairs, int[] array) {
        for(int i=0;i<pairs.length;i++) {
            Pair p = new Pair();            
            p.occurance_num = 1;
            p.value = array[i];
            p.useDuringCount = true;
            pairs[i] = p;
        }
    }
    
    private static void printPairsAtrray(Pair[] pairs) {
        System.out.println("--------------------");
        for(int i=0;i<pairs.length;i++) {
            System.out.println("pairs["+i+"].occurance_num="+pairs[i].occurance_num);
            System.out.println("pairs["+i+"].value="+pairs[i].value);
            System.out.println("pairs["+i+"].useDuringCount="+pairs[i].useDuringCount);
            System.out.println("--------------------");
        }
    }
     
    private static int calculateDuplicatesNumber(Pair[] pairs) {
        System.out.println("-------------------- Duplicates:");
        int duplicates_num = 0;
        for(int i=0;i<pairs.length;i++) {
            if(pairs[i].occurance_num > 1) {
                duplicates_num++;
                System.out.println("number: "+pairs[i].value+" occurance_num " + pairs[i].occurance_num);
            }
        }
        return duplicates_num;
    }        
}

class Pair {
    int value;
    int occurance_num;
    boolean useDuringCount;
}

Комментариев нет:

Отправить комментарий