{6,12,4,7,9}


How do i write a method name serachMax that receives the list above (as an array of integer) as it's argument and return the largest value in the list

and

How do i create a method name countitem that receives that list above (as a referenced based linked list of integers) as it's argument and returns the largest value in the list

Dani AI

Generated

This thread asks for two small utilities: one that returns the largest value from an int[] and one that does the same for a reference-based linked list. asked for an attempt, suggested sorting (bubble sort), and and pointed toward loop-based solutions. Sorting is unnecessary for this task — a single linear pass finds the maximum in O(n) time with O(1) extra space.

A safe, minimal array implementation (corrects the misspelling serachMax) — linear scan with input checks:

public static int searchMax(int[] a) {
    if (a == null || a.length == 0) {
        throw new IllegalArgumentException("array is null or empty");
    }
    int max = a[0];
    for (int i = 1; i < a.length; i++) {
        if (a[i] > max) {
            max = a[i];
        }
    }
    return max;
}

For a reference-based singly linked list, a while-loop is the natural approach. Note that the method name countitem from the original question is misleading; a clearer name is findMax or maxInList. Example using a simple Node:

static class Node {
    int value;
    Node next;
    Node(int v, Node n) { value = v; next = n; }
}

public static int countItem(Node head) {
    if (head == null) throw new IllegalArgumentException("list is empty");
    int max = head.value;
    Node cur = head.next;
    while (cur != null) {
        if (cur.value > max) max = cur.value;
        cur = cur.next;
    }
    return max;
}

Notes: use the array version for primitive arrays (avoids boxing). For java.util.List<Integer> prefer for-each or streams (e.g., list.stream().mapToInt(Integer::intValue).max()), and always handle null or empty inputs explicitly. Sorting the whole collection (bubble sort or otherwise) wastes work when only the maximum is required.

Recommended Answers

All 4 Replies

Why not post your attempt ? Read this.

Member Avatar for Member #46692

Hint: Search for bubble sort.

Once you've successfully done that, then you can start to think about creating a function or method for it.

you already wrote that method name in your question, thereby answering your own question.

{6,12,4,7,9}
How do i write a method name serachMax that receives the list above (as an array of integer) as it's argument and return the largest value in the list

Hint: for loop

How do i create a method name countitem that receives that list above (as a referenced based linked list of integers) as it's argument and returns the largest value in the list

Hint: while loop

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.