Skip to main content

Most Frequent Element in the Array

Loop Pointer Tracer
Java · DSA · Brute Force

Most frequent element — watch the pointers move

The same brute-force logic as before, now animated: two colored pointers slide across the array as i and j advance, a beam lights up teal on a match and rose on a mismatch, and the counters pop the instant they change.

01 The code being traced

for (int i = 0; i < n; i++) {
    int count = 0;
    for (int j = 0; j < n; j++) {
        if (arr[j] == arr[i]) {
            count++;
        } else if (maxCount < count) {
            maxCount = count;
            element = arr[i];
        }
    }
}
System.out.print(element);

02 Live pointer tracer

Amber pointer = i. Teal pointer = j. The beam between them turns teal on a match, rose on a mismatch — watch how often it stays rose right before the answer should update.

๐ŸŽฌ Comparison tracer
Load an array, then step, autoplay, or click any row in the table below to jump straight to it.
i
j
i = –
j = –
count = 0
maxCount = 0
element = 0
comparisons so far = 0
Load an array, then press Step ▶ to begin.
0 / 0 comparisons0%
Program printed–
Actual most frequent value–
Verdict–
Speed

03 Every comparison, in order

Click any row to jump the tracer above straight to that comparison. Rows in rose are where the inner loop ends on a match — the exact moment a real update gets skipped.

#ijarr[i]arr[j]BranchcountmaxCountelement
What to watch for: the comparison against maxCount only runs inside the else if branch — triggered by a mismatch. Whenever the inner loop's last tick happens to be a match, that final count is never checked, and maxCount silently stays behind the true frequency.

04 Complexity

MetricValue
TimeO(n²) — for every i, scan all j
SpaceO(1) extra — no hash array, no auxiliary storage

The "comparisons so far" counter above grows exactly like the progress bar suggests — every extra element added to the array multiplies the remaining work, not just adds to it.

05 Quick revision

1

Two nested loops, zero extra memory

i is the candidate value, j re-scans the whole array to count its occurrences.

2

element = arr[i], not i

The winner stores the actual repeated value, never the index it was found at.

3

The update lives in the mismatch branch

maxCount only changes inside else if — a match alone never triggers it.

4

Last-tick matches get skipped

If j's final tick is a match, that count has no mismatch left to compare it.

5

Trace before you trust

Any array where the true mode sits at the end (like [1, 2, 2]) exposes the bug immediately.

6

The fix: compare once per i, after j finishes

Moving the check outside the inner loop guarantees every count gets compared exactly once.

Interactive reference for the brute-force "most frequent element" pattern in Java-based DSA practice.

Comments

Popular posts from this blog

Get Keycloak Auth Access Token

Understanding Keycloak Auth Access Token – A Deep Dive $ keycloak / auth-deep-dive ๐Ÿ” Identity & Access Management Getting a Keycloak Access Token — and Actually Understanding It June 2025 Keycloak OAuth 2.0 JWT OpenID Connect Service Account You hit Keycloak's token endpoint, you get back a fat JSON blob — but what is all that stuff? This post dismantles a real token response piece by piece so you know exactly what you have, why the JWT is structured the way it is, and what to do with it next. ๐Ÿ“ก Step 1 — How to Get the Token Keycloak speaks OAuth 2.0 . The endpoint that issues tokens lives under your realm: HTTP Token Endpoint POST http://127.0.0.1:7080/realms/master/protocol/openid-connect/token Content-Type : application/x-www-form-urlencoded grant_type =client_credentials &client_id =eazybank-callcenter-cc ...

Observability & Monitoring through Loki,Promtail (Alloy),Prometheus,Micrometer in Grafana

๐Ÿ”ง What this demo covers End-to-end observability setup using Prometheus + Loki + Grafana Integration of Micrometer with Spring Boot for real-time metrics Log collection using Promtail / Alloy from application containers ๐Ÿ“Š Metrics Monitoring (Prometheus) Scraping metrics from /actuator/prometheus endpoint JVM metrics: memory, threads, GC activity HTTP metrics: request count, latency, error rates Custom metrics via Micrometer ๐Ÿ“œ Centralized Logging (Loki + Promtail) Aggregates logs from multiple microservices Label-based log filtering (fast & efficient) No heavy indexing → lightweight compared to ELK ๐Ÿ“ˆ Visualization (Grafana Dashboards) Real-time dashboards for metrics & logs Correlate logs with metrics for faster debugging Pre-built + custom dashboards ⚙️ Architecture Flow Spring Boot app → exposes metrics via Micrometer Prometheus → scrapes & stores metrics Promtail/Alloy → collects logs → pushes to Loki...