Dain1234 / Java-Programming

0 stars 0 forks source link

Frequency of elements in an array #10

Open Dain1234 opened 1 year ago

Dain1234 commented 1 year ago

package com.dain.logics;

public class frequencyinarray { public static void main(String[] args) { int a[] = new int[] { 1, 2, 8, 3, 2, 4, 4, 5 }; int t[] = new int[a.length]; int visited = -1; for (int i = 0; i < a.length; i++) { int count = 1; for (int j = i + 1; j < a.length; j++) {

            if (a[i] == a[j]) {
                count++;
                t[j] = visited;
            }

        }
        if (t[i] != visited) {
            t[i] = count;
        }

    }
    for (int i = 0; i < a.length; i++) {
        if (t[i] != visited) {

            System.out.println(a[i] + "   " + t[i]);
        }
    }

}

}