0

In an array i want to count all duplicates and to write how many times they are repeated, how should i do it using only loops

array example : int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};

This is my code

public static void main(String[] args) {
        int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
        int cntPoz = 0, cntNeg = 0;

        //for petlja broji pozitivne i negativne clanove array-a
        for (int a = 0; a < array.length; a++) {
            if (array[a] > 0) {
                cntPoz++;
            } else if (array[a] < 0) {
                cntNeg++;
            }
        }

        // kreiranje novih nizova 
        int[] pArr = new int[cntPoz];
        int[] nArr = new int[cntNeg];
        //for petlja iz array-a premjesta sve pozitivne brojeve
        cntPoz = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > 0) {
                pArr[cntPoz++] = array[i];
            }
        }

        //for petlja iz array-a premjesta sve negativne brojeve
        cntNeg = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < 0) {
                nArr[cntNeg++] = array[i];
            }
        }

        //sortiranje i ispis nizova
        Arrays.sort(pArr);
        Arrays.sort(nArr);
        System.out.println("Originalni niz je : \n" + java.util.Arrays.toString(array) + "\n");
        System.out.println("Niz pozitivnih brojeva : \n" + java.util.Arrays.toString(pArr) + "\n");
        System.out.println("Niz negativnih brojeva : \n" + java.util.Arrays.toString(nArr));
        System.out.println();
        System.out.println();


        // for petlja prebrojava sve duplikate i ispisuje ih
        int dupCnt = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {

                if ((array[j]) == array[i]) {
                    dupCnt++;
                    System.out.println("Duplicates  " + array[j]);
                }
            }
        }
        System.out.println("Number of duplicates  : " + dupCnt);
LjubisaK
  • 9
  • 1
  • 8
  • 4
    Show us what you have tried and we will try to point you in the right direction! – Jite Dec 21 '16 at 20:58
  • Possible duplicate of [Java + Count duplicates from int array without using any Collection or another intermediate Array](http://stackoverflow.com/questions/31738717/java-count-duplicates-from-int-array-without-using-any-collection-or-another-i) – firephil Dec 21 '16 at 22:05
  • Do you know what a hashtable is? – mba12 Dec 21 '16 at 22:33
  • @Jite this is where i find duplicates int dupCnt = 0; for (int i = 0; i < array.length; i++) { for (int j = i+1; j < array.length; j++) { if ((array[j]) == array[i]) { dupCnt++; System.out.println("Duplicates " + array[j]); } } } System.out.println("Number of duplicates : " + dupCnt); – LjubisaK Dec 21 '16 at 23:12
  • @mba12 no i dont i have just started with java and im trying to do it with loops, so is there a way? – LjubisaK Dec 21 '16 at 23:13

1 Answers1

0

Try this code. It sorts array of integers first in incrementing order. Then counts the distance between the same numbers and puts them into map with key-number and value-occuarence. Printing format is not that great but is done just for check

import java.util.*;
 public class Duplicates {
  public static void main(String[] args)
    {
    Integer[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87, 12};
    mergeSort(array);
    for(Integer x : array)
    {
        System.out.println(x);
    }
    Map<Integer,Integer> duplicates = new HashMap<Integer,Integer>();
    int count = 1;
    for(int i = 0;i < array.length-1;i++)
    {
        if(array[i].equals(array[i+1]))
        {
            count++;
        }
        else
        {
            duplicates.put(array[i],count);
            count=1;
        }
    }
    for ( Integer key : duplicates.keySet() ) 
    {
       System.out.println("Number: " + key + " occuars " + duplicates.get(key) + " times" );
    }        
    }
    public static void mergeSort(Integer[] a)
    {
       if(a.length>1)
       {
       int i,mid = a.length/2;
       Integer[] half1 = new Integer[mid];
       Integer[] half2 = new Integer[a.length-mid];
       for(i=0; i<mid; i++)
       half1[i]=a[i];
       for(; i<a.length; i++)
       half2[i-mid]=a[i];
       mergeSort(half1);
       mergeSort(half2);
       int j=0, k=0;
       for(i=0; j<half1.length&&k<half2.length; i++)
       if(half1[j].compareTo(half2[k])<0)
       {
         a[i]=half1[j];
         j++;
       }
       else
       {
         a[i]=half2[k];
         k++;
       }
       for(; j<half1.length; i++, j++)
       a[i]=half1[j];
       for(; k<half2.length; i++, k++)
       a[i]=half2[k];
 }
 }
}