HackerHeap

Largest Unique Number Java Solution

// BUILDING THIS WITH AN AI AGENT (2026)

Whether you are using Claude Code, Cursor, Windsurf, GitHub Copilot, Gemini Code Assist, Aider, or Cline — the productive pattern for production code in this space is: scaffold first, then ask for the idiomatic refactor. The prompt sequence:

  1. Scaffold. “Generate a basic working implementation. Don’t worry about elegance. Just get the happy path correct.”
  2. Ask for idiomaticity. “Refactor this to be idiomatic for the framework version I’m using. Specifically use [records / constructor injection / @Configuration / proper exception types / etc].”
  3. Test generation. “Add tests covering the happy path and 3 realistic edge cases. Use the testing conventions of the codebase.”
  4. Document the public API. “Add doc-comments on the public methods explaining what callers can and can’t assume.”

Agents are good at idiomatic code when you explicitly ask — they default to older patterns otherwise.

Question :

Given an array of integers A, return the largest integer that only occurs once.

If no integer occurs once, return -1.

Example 1:

Input: [5,7,3,12,4,12,11,3,1]
Output: 11
Explanation: 
The maximum integer in the array is 12 but it is repeated. The number 11 occurs only once, so it's the answer.

Example 2:

Input: [9,9,8,8]
Output: -1
Explanation: 
There is no number that occurs only once.

Note:

  1. 1 <= A.length <= 2000
  2. 0 <= A[i] <= 1000

Solution:




To find the largest unique number , first we will take an array of size 1001 as the note says the maximum array length would be 1000, we will loop through the given array and increase the index in the newly created array, once out pass through the array, we parse the newly created array from i=1000 to 0 and would return the first index of the array whose value is 1.

class Solution {
    public int largestUniqueNumber(int[] A) {
        int [] res = new int [1001];
        for(int i = 0; i < A.length; i++){
            res[A[i]]++;
        }
        for(int i = 1000; i >= 0; i--){
            if(res[i] == 1){
                return i;
            }
        }
        return -1;
    }
}




This is the Largest Unique Number Java Solution in the below post.


For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.


Tagged: