0

I found this line very confusing:

return new int[] {};

Are the two curly brackets referencing something in memory? Here's the full code:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> prevMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int diff = target - num; //En diff, iremos restando el num al Objetivo

            if (prevMap.containsKey(diff)) { 
                return new int[] { prevMap.get(diff), i };
            }

            prevMap.put(num, i);
        }

        return new int[] {};
    }
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    That's just the shortcuts syntax [to create an array](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). An empty array in this case. And `new int[] {1, 2, 3}` would create an array containing those 3 integers. – OH GOD SPIDERS Aug 11 '23 at 14:32
  • Aah thanks so much @OHGODSPIDERS , kinda figured it out after asking lmao but understood perfectly now! – SleepyProgrammingN0ises Aug 11 '23 at 14:36

1 Answers1

0

It simply returns an empty int array as function output.

Lore
  • 1,286
  • 1
  • 22
  • 57