I am new to programming, so please accept my apologies if this question is simple.

My teacher wants us to create a method that searches a stack for the name that occurs most often and returns it

for example : If I have these names in my stack:

Tiger Woods, Jack Nicholas, Arnold Palmer, Jack Nicholas, Jimmy Demerrit, Jack Nicholas, Sam Sneed, Jimmy Demerrit, Ben Hogan, Walter Hagen, Tommy Armor, Bobby Jones

the method should return Jack Nicholas

Please help me for I need to know how to do it for my program

thank you in advance

Dani AI

Generated

A short, practical plan plus two safe ways to implement it. As suggested, begin with a short pseudocode outline; 's push to implement and test is sound; pointed out forum rules, so this note explains the method and shows a concise, instructive Java example rather than a lengthy drop-in solution.

Recommended (fast and simple): count occurrences with a Map. If the original Stack must be preserved, pop every element into a temporary stack while updating counts, then restore the original stack. Time O(n), extra space O(n). The example below preserves the stack and returns null when the stack is empty.

import java.util.*;

public static String mostFrequentPreserve(Stack<String> stack) {
    if (stack == null || stack.isEmpty()) return null;
    Map<String,Integer> counts = new HashMap<>();
    Stack<String> temp = new Stack<>();
    String maxName = null;
    int maxCount = 0;
    while (!stack.isEmpty()) {
        String s = stack.pop();
        temp.push(s);
        int c = counts.getOrDefault(s, 0) + 1;
        counts.put(s, c);
        if (c > maxCount) {
            maxCount = c;
            maxName = s;
        }
    }
    while (!temp.isEmpty()) {
        stack.push(temp.pop());
    }
    return maxName;
}

If extra collections are not allowed (some instructors require only stack operations), use an O(n^2) approach: for each popped element, scan the remaining stack to count matches (moving elements to another temp), restore the stack, and track the maximum. That uses only stacks but is slower.

Notes and gotchas: decide on case sensitivity and trimming (call trim()/toLowerCase() before counting if needed); decide how to break ties (current code keeps the first name that reaches the top count during the pass); prefer Deque (ArrayDeque) in modern code when a simple stack is needed instead of Stack. Tests should include empty stack, single element, ties, and names with different capitalization.

Recommended Answers

All 4 Replies

so now is the time to plan your program and write some code. If you experience problems post the code snippet and someone will help you fix it.

I'm new to programming, can you please show an example

no, we would be violating forum rules.
you may want to take a close look at the first thread in this forum, it contains a lot of helpfull links and information to help you get started.

before writing code, come up with a plan of what you want to do, obviously theres no function that returns the value that comes back the most often in a list, otherwise it wouldnt be an assignement , so pick up and pen and paper, take your list, and find which name comes up the most often while writing everything you do in your head on the paper .

now you've got pseudocode, you can start writing code from that, or post your pseudocode here and ask us if the logic behind it is flawed, if it is, and we see it , we could save you some time trying to debug code that comes from flawed pseudocode.

Looking foward to see your pseudocode tomorrow morning, im going home for now, good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.