Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum I mistakenly posted this elsewhere too.
I have a file like this: field 2 values are either 0 or negative. file test4:

100815    -20
118125    0
143616    0
154488    0
154488    0
154488    -6
196492    -5
196492    -9
196492    -7
27332    0
29397    0

I would like to print a line containing the maximum value in field 2 of all occurrences for each value in field 1. So the desired output should be:

29397    0
27332    0
143616    0
154488    0
118125    0
100815    -20
196492    -5

I am using awk to do this. But, there are two problems: first awk prints nothing when I try to get the maximum for the values with negative numbers but it has no problem when I try the same with positive numbers. This made me take the absolute values which I can later turn back to the original values. The second problem, I get the following output with my code:

29397    0
27332    0
143616    0
154488    6
118125    0
100815    20
196492    7

Although with absolute values I should get:

29397    0
27332    0
143616    0
154488    0
118125    0
100815    20
196492    5

My code is:

awk '{$2>0?$2=$2:$2=-$2} $2==0 {$2=0} {print}' test4 | 
   awk 'NR==1 {a[$1]=$2} {a[$1]=$2 ; if ($2<a[$1]) a[$1]=$2; else a[$1]=a[$1];} 
  END {for(i in a) print i"\t"a;}'

I am sure I'm missing something basic and this could probably be done in a much simpler way. Any help is appreciated
Best reagrds to all

This is your original post, showing each dataset on its own line.

Hi everyone, I'm a new member at the forum
I have a file like this: field 2 values are either 0 or negative. file test4:

100815    -20
118125    0
143616    0
154488    0
154488    0
154488    -6
196492    -5
196492    -9
196492    -7
27332    0
29397    0

I would like to print a line containing the maximum value in field 2 of all occurrences for each value in field 1. So the desired output should be:

100815    -20
118125    0
143616    0
154488    0
196492    -5
27332    0
29397    0

I am using awk to do this. But, there are two problems: first awk prints nothing when I try to get the maximum for the values with negative numbers but it has no problem when I try the same with positive numbers. This made me take the absolute values which I can later turn back to the original values. The second problem, I get the following output with my code:

100815    20
118125    0
143616    0
154488    6
196492    7
27332    0
29397    0

Although with absolute values I should get:

100815    20
118125    0
143616    0
154488    0
196492    5
27332    0
29397    0

My code is:

awk '{$2>0?$2=$2:$2=-$2} $2==0 {$2=0} {print}' test4 | awk 'NR==1 {a[$1]=$2} {a[$1]=$2 ; if ($2<a[$1]) a[$1]=$2; else a[$1]=a[$1];} END {for(i in a) print i"\t"a;}'

I am sure I'm missing something basic and this could probably be done in a much simpler way. Any help is appreciated
Best reagrds to all

awk '
{
        if ( $1 in A )
        {
                if ( $2 > A[$1] )
                        A[$1] = $2
        }
        else
                A[$1] = $2
} END {
        for ( i in A )
                print i, A
} ' file

I understand that it gives the required output.!

But, Why is the order changed while printing output?

Thank you so much.

That is because by default, the order in which a for (i in array) loop scans an array is not defined; it is generally based upon the internal implementation of arrays inside awk.