How does the latin root of "egregious" relate to its English definition?

The latin root of the word egregious is grex meaning flock which is also the root of many other English words that deal with groups or "flocks" (usually of people): aggregate, congregate, segregate, gregarious...

But I don't see how the definition of egregious has anything to do with a flock or group.

egregious [ih-gree-juh s] adjective

  1. extraordinary in some bad way; glaring; flagrant: an egregious mistake; an egregious liar.
  2. Archaic. distinguished or eminent.

Solution 1:

Egregious (adj)

  • 1530s, "distinguished, eminent, excellent," from Latin egregius "distinguished, excellent, extraordinary," from the phrase ex grege "rising above the flock," from ex "out of" (see ex-) + grege, ablative of grex "a herd, flock"

Disapproving sense, now predominant, arose late 16c., originally ironic. It is not in the Latin word, which etymologically means simply "exceptional."

(Etymon)