So, if I'm correct, I believe that a monster that has spellcasting gets its spells as stated by the class. If so, then take the 18th level wizard.
NPCs don't have to follow PC rules, but you might have misunderstood the monster spell lists anyway
As ravery points out, it's not necessary for NPCs to perfectly align to PC class descriptions. An NPC wizard or monster with spellcasting ability doesn't have to follow the same rules that a PC wizard does, even if the monster seems like it should be fairly described as a "wizard".
However, it seems like you might be misunderstanding the difference between spells known/prepared and spell slots. As the Wizard class describes:
The Wizard table shows how many spell slots you have to cast your spells of 1st level and higher. To cast one of these spells, you must expend a slot of the spell’s level or higher. You regain all expended spell slots when you finish a long rest.
You prepare the list of wizard spells that are available for you to cast. To do so, choose a number of wizard spells from your spellbook equal to your Intelligence modifier + your wizard level (minimum of one spell). The spells must be of a level for which you have spell slots.
As 18th level casters, both the Archmage and the Lich have the same number of spell slots – including one 8th level slot. As it happens, the progression of spell slots is the same for every full spellcasting class (bard, cleric, druid, sorcerer, and wizard), so "an Xth level spellcaster" is enough to describe how many spell slots a creature should have – it's not necessary to say "an Xth level wizard".
However, the Lich and the Archmage have a different list of prepared spells, and you may note that there is no limitation that you can only prepare a certain number of spells of certain levels. An 18th level wizard with a +5 intelligence modifier has the capacity to prepare up to 23 spells of any level they so choose, and they could prepare 23 9th level spells if they knew that many (though that would obviously be a very poor use of their capacity). It just so happens that the Lich has chosen to prepare more higher-level spells than the Archmage – it doesn't have the ability to actually cast more high-level spells in a given period, it just has a greater choice of high-level spells available (and consequently less choice of lower level spells).
As a side note, the Lich has 23 spells memorised, which is exactly in line with an 18th level wizard with that intelligence score, but it doesn't benefit from any of the other class features that a wizard has, like Spell Mastery. The Archmage only has 20 spells listed as prepared, which is fewer than you might expect – but it also has the ability to cast disguise self and invisibility at will, exactly as if it had prepared those spells with the Spell Mastery wizard class feature – which gives a total of 22 spells, still one shy of what a PC wizard could prepare but pretty close. It's probably fair to say that these two NPCs/monsters were designed with reference to the normal rules of wizard spellcasting, but they are not meant or required to be strictly adherent to those rules.
Monsters don't conform to character classes. Both the Lich and Archmage differ from an 18th level wizard with 20 Int.
An 18th level wizard with 20 intelligence would have 23 spells prepared, a spell DC of 19, and a spell attack modifier of +11. (PHB p.114)
(Questions about why monsters don't conform to character classes would be designer intent questions and off-topic here.)