Skip to main content

Posts

Recent posts

Topic- (Hashing) Second Highest Occuring/Frequency Element Brute Force Approach

Java · DSA · Brute Force ◐ Find the element with the second-highest frequency using a visited array and two leader slots, el1 and el2 . Read the code, then watch it run step by step on your own input. 01 Problem 02 Code 03 How it works 04 Tracer 05 Complexity 06 Revision 07 Quiz 01 The problem Given an array of N non-negative integers, print the element that occurs the second most times. If no element takes second place, print -1 . For [1, 2, 2, 3, 3, 3] , the frequencies are 1 → 1, 2 → 2 and 3 → 3. Value 3 occurs most, so the answer is 2 . Type an array to see what the program prints and how often each value occurs. Array (spaces or commas) Run 02 The code Walk the array with i . The first time a value shows up, mark it in visited and count its occurrences with an inner j loop. Then compare that count with the two leaders: el1...