Sex is not so much an issue as your familiarity with the person.
In the case where you are friends but not that close (Extension from them not telling you that they are trying to lose weight) The best and most socially common line is:
"You're looking really good these days." You can add some salt and pepper like a "wow" at the start, but that is the basis.
If you are closer and they have shared the fact that they are on a diet, then it might be okay to make a direct comment like "That diet is really working" or "wow you have lost a lot of weight" but in general even these are kind of awkward because they lead to a trap:
1: Wow that diet is really working, you are looking great
2: I still have a long way to go
This is a trap because either you say they look good now and they are not happy themselves, then you are a liar in their eyes.
And if you say agree (You have said you still think they are overweight)
Now there are some recoverable lines here, and again it depends on familiarity, but in general it is probably best to avoid the issue directly and give them a general comment about their aesthetic.
Either way it is important to understand the purpose and use of compliments and when to use them. And as a general rule the first option is the standard social interaction for almost all scenarios I can think of here.