I have the following data table x

```
id1 id2
a  x
a  x
a  y
b  z

```

For each combination of id1, id2 I can find the number of instances in the following way

```
x[,list(
    freq = .N
   ),by = "id1,id2"]

```

The above would yield

```
a x 2
a y 1
b z 1

```

Next I want to find the most frequent id2 for each id1, i.e. mode. So the expected result is 

```
 a x 2
 b z 1

```

I can get there in a round about way, but is there a way to put a sequence number at the id1 level? Or some such hack that gets me to this efficiently and quickly, perhaps at the first step shown above?
Thanks in advance