Program to find the occurrences of numbers in an array.

 Problem:- Program to find the occurrences of numbers in an array.




Input:-  {0, 1, 0, 3, 5, 4, 2, 1, 3}

Output:- 

0    ---->2

1    ---->2

3    ---->2

5    ---->1

4    ---->1

2    ---->1


Solution:- 






package com.codeforsolution.logical.java;
/**
 * Program to find the occurrences of numbers in array.
 * input :- {0, 1, 0, 3, 5, 4, 2, 1, 3}
 * output
 * 0    ---->2
 * 1    ---->2
 * 3    ---->2
 * 5    ---->1
 * 4    ---->1
 * 2    ---->1
 */
public class FindOccurrencesTest {

    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 5, 4, 2, 1, 3};
        int[] noDuplicate = new int[nums.length];
        int count = -1;
        for(int i = 0; i < nums.length; i++){
            int localCount = 1;
            for(int j = i+1; j < nums.length; j++){
                if(nums[i] == nums[j]){
                    localCount ++;
                    noDuplicate[j] = count;
                }
            }
            if(noDuplicate[i] != count){
                noDuplicate[i] = localCount;
            }
        }

        for (int k = 0; k < noDuplicate.length; k++){
            if(noDuplicate[k] != count){
                System.out.println(nums[k] +"    ---->"+ noDuplicate[k]);
            }
        }
    }
}

Github link for the codes  Find occurrences of numbers in an array

Post a Comment (0)
Previous Post Next Post