Skip to main content

Posts

Most Frequent Element in the Array

Most Frequent Element ◐ Java · DSA · Hashing Most frequent element — from O(N²) to O(N) , pointer by pointer Three ways to find the element that appears most often: brute force, array hashing and a HashMap. Each approach has an animated tracer, the two common bugs are shown as they happen, and the complexity is worked out correctly. 01 Problem 02 Brute force 03 Array hashing 04 Complexity 05 HashMap 06 Revision 07 Quiz 01 The problem Given an array of N integers, print the element that appears most often. For [1, 3, 2, 3, 1, 3] the answer is 3 , which appears 3 times. Type any array to run every version at once. Some presets are chosen to break one of them. Array (spaces or commas) Run all 02 Approach 1: Brute force For every index i , scan the whole array with j and count how often arr[i] appears. Keep the best count. ...

Frequencies of Characters By Brute Force Vs Hashing

Character Frequencies — Hashing vs Brute Force Java · DSA · Character Hashing Character frequencies — hashing vs brute force Same problem, two solutions, wildly different scaling. Type a string below and watch both approaches work in real time. 🔤 The 26-bucket hasher Every lowercase letter maps straight to hash[ch - 'a'] — no scanning required. String input Query characters Hash it 01 The two implementations Both take a string and answer "how many times does character X appear?" — but they get there very differently. Hashing.java BruteForce.java import java.util.Scanner; public class CharacterFrequenciesByHashing { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char [] arr = new char [n]; int [] has...

Frequencies of Array By Hashing Technique (Optimized Approach)

Frequencies by Hashing — an interactive walkthrough Java · DSA · Hashing Counting frequencies by hashing — and where the array should live A working int[] hash-array solution, traced against three inputs, plus the actual reason people move big arrays outside main() . Play with the bucket hasher below before you read a line of code. 🪣 The bucket hasher Type an array — watch it drop into indexed buckets, live. Array (space separated) Queries (space separated) Hash it Every value becomes an index. hash[value]++ drops a ball in that bucket — no comparisons, no searching, just direct addressing. That's the whole trick. 01 The working solution This is a correct, minimal frequency counter. It sizes hash to n + 1 , which works cleanly as long as every value fits within that range. FrequenciesByHa...