2

I know how to go through whole array, but I only need number of duplicate occurrences. I'm at beginners level, so just basic use of loops and arrays.

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

for (int i = 0; i < array.length; i++) {
    int count = 0;
    for (int j = 0; j < array.length; j++) {
        count++;   
    }
    System.out.println(array[i] + "\toccurs\t" + count + "X");
}
4castle
  • 32,613
  • 11
  • 69
  • 106
SeeSee
  • 57
  • 7
  • 2
    Possible duplicate of [find repeated elements and count of their](http://stackoverflow.com/questions/17630727/find-repeated-elements-and-count-of-their) – Matthew Brzezinski Nov 18 '16 at 21:09
  • @user123 this is excelent solution but we haven't got to the "input" part i school – SeeSee Nov 18 '16 at 21:21

3 Answers3

2

You can do better if you use more than just loops and arrays, but a simple algorithm would be to use two nested for loops, and put an if statement inside that increments a counter when a duplicate is found.

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

for (int i = 0; i < array.length - 1; i++) {
    int count = 1;
    for (int j = i + 1; j < array.length; j++) {
        if (array[i] == array[j]) {
            count++;
        }
    }
    if (count > 1) {
        System.out.println(array[i] + "\toccurs\t" + count + " times");
    }
}
4castle
  • 32,613
  • 11
  • 69
  • 106
0
using System;

public class Exercise34
{
    public static void Main()
    {
        int[] a = { 3,4,5,6,7,8,3,4,5,6,7,8,9,9};
        int n = a.Length-1;
        int dupcounter = 0;
        for (int i = 0; i < n; i++)
        {
            int counter = 0;
            for (int j = i + 1; j <= n; j++)
            {
                if (a[i] == a[j])
                {
                    counter++;
                    n--;
                    if (counter == 1)
                    {
                        dupcounter++;
                        Console.WriteLine(a[i]);                        
                    }
                    for (int k = j; k <= n; k++)
                    {
                        a[k] = a[k + 1];
                    }
                }
            }
        }
        Console.WriteLine(dupcounter);
    }
}
  • Code is trying to reduce the size of an array once it finds replication of a number. If I have understood the question correctly, we all have to look for number of duplicate occurrence not how many times it present. Once all occurrences are removed, we are going to iterate till 'N' reduced times and print all the numbers. (Note: No need to sort this out) – Sekar Nagaraj Feb 13 '19 at 06:14
0

For String I print all the duplicates and their counts. Please check above code and my logic.

package Interview_Strings;

import java.util.Arrays;

public class print_duplicates_and_their_counts 
{
    static void printDupCount(String str)
    {
        char[] charString=str.toCharArray();
        Arrays.sort(charString);
        int size=charString.length;
        int j=1,i=0;
        int count=0;
        while(i<size && j<size)
        {
            if(charString[i]!=charString[j])
            {
                count=j-i;
                System.out.println(charString[i]+" : "+count);
                i=j;
                j++;
                if(j>=size)
                {
                    System.out.println(charString[j-1]+" : "+count);

                }
                
                
            }
            else
            {
                j++;
            }
            
        }
    }
    public static void main(String args[])
    {
        String str="bigbangtheory";
        printDupCount(str);
    }

}

output: a : 1 b : 2 e : 1 g : 2 h : 1 i : 1 n : 1 o : 1 r : 1