JEFFERSON CITY, Mo. — Democrats won more votes, regained control of the U.S. House and flipped hundreds of seats in state legislatures during the 2018 elections. It was, by most accounts, a good year for the party.

Yet it wasn't as bad as it could have been for Republicans.

That's because they may have benefited from a built-in advantage in some states, based on how political districts were drawn, that prevented deeper losses or helped them hold on to power, according to a mathematical analysis by The Associated Press.

The AP's analysis indicates that Republicans won about 16 more U.S. House seats than would have been expected based on their average share of the vote in congressional districts across the country. In state House elections, Republicans' structural advantage might have helped them hold on to as many as seven chambers that otherwise could have flipped to Democrats, according to the analysis.

The AP examined all U.S. House races and about 4,900 state House and Assembly seats up for election last year using a statistical method of calculating partisan advantage that is designed to flag cases of potential political gerrymandering. A similar analysis also showed a GOP advantage in the 2016 elections.

The AP used the so-called "efficiency gap" test in part because it was one of the analytical tools cited in a Wisconsin gerrymandering case that went before the U.S. Supreme Court in 2017 and is part of a North Carolina case scheduled to be argued on Tuesday before the court. In that case, justices will decide whether to uphold a lower court ruling that struck down North Carolina's congressional districts as an unconstitutional political gerrymander favoring Republicans.

The high court also is to hear arguments Tuesday on whether Democrats in Maryland unconstitutionally gerrymandered a congressional district in 2011 in order to defeat a long-time Republican incumbent.

The U.S. Supreme Court has never struck down districts because of excessive partisan manipulation, and the efficiency gap formula is no guarantee it will start. During arguments on the Wisconsin gerrymandering case, Chief Justice John Roberts called it "sociological gobbledygook."

Some Republicans also have criticized it, insisting they win simply because they run better candidates. The formula does not necessarily prove political shenanigans, because partisan advantages also can arise naturally based on where Democratic and Republican voters choose to live.

Many political and redistricting experts say the formula provides a neutral way to determine the effects of gerrymandering and how one party can maintain power for a decade or beyond. Plaintiffs in the North Carolina case say now is the time for the court to end highly partisan gerrymandering, with the next round of redistricting set to follow the 2020 Census.

"Gerrymandering as a whole cheats voters out of our representation," said Love Caesar, a student at North Carolina A&T State University who works with Common Cause, an advocacy group that is a lead plaintiff in the case.

The AP's analysis found North Carolina Republicans won two or three more congressional seats than would have been expected based on their share of the vote. Republican candidates received 51 percent of the two-party vote compared to Democrats' 49 percent. Yet Republicans won a 9-3 seat advantage over Democrats, with one seat still undecided because of allegations of vote fraud.

Democrats say that illustrates the effect of Republican gerrymandering and point to Caesar's university, a historically black college in Greensboro, as an example. Republicans in the General Assembly divided the school when they drew the congressional map, dispersing a Democratic-leaning voting bloc among two Republican-leaning districts that extend from Greensboro into more rural areas.

The congressional districts that split the campus are both represented by Republicans.

"It's hard to explain to students who are already skeptical about the voting process ... that the state intentionally diluted their power in voting by putting this line back here in between our campus," said North Carolina A&T student Kylah Guion, who also works with Common Cause.

Republican lawmakers concentrated Democrats in other congressional districts. In the only three districts Democrats won last November, they carried at least 70 percent of the two-party vote.

The efficiency gap test offers a way to assess the effects of redistricting strategies — packing voters of one party into some districts, or spreading them out among others to make it easier for the other party to win seats. It compares a party's average district vote share to the share of seats it wins.

For the 2016 and 2018 elections, North Carolina had the highest pro-Republican tilt among the roughly two dozen largest states that determine the bulk of the seats in the U.S. House, according to the AP's analysis.

Other states that had consistently sizable Republican advantages in both congressional elections included Ohio, Indiana, Wisconsin, Alabama and Texas — all places where Republicans were in charge of redistricting after the 2010 Census. Although Democratic victories mounted nationally in 2018, the AP's analysis showed that the Republicans' efficiency gap advantage became more pronounced in those states.

Massachusetts showed a consistent Democratic tilt in its congressional districts, though not at the same magnitude as the most pro-Republican states.

In Pennsylvania, the Democratic-majority state Supreme Court redrew the congressional map for the 2018 elections after striking down the previous Republican-drawn version as an unconstitutional partisan gerrymander. The AP's analysis found the Republican tilt was cut by more than half from the 2016 to 2018 elections as the state's congressional delegation went from a 13-5 GOP majority to an even split of nine seats each for Republicans and Democrats.

The analysis shows Pennsylvania Democrats could have expected to win a 10-8 congressional majority based on getting about 55 percent of the total two-party vote. The fact they didn't might be explained by the high concentration of Democrats in urban areas, which diminishes their votes elsewhere.

The AP's analysis also found a persistent Republican advantage in state House or Assembly districts in Florida, Michigan, Wisconsin and South Dakota and a consistent Democratic edge in Nevada.

There were five state legislative chambers where Republicans retained the majority in 2018 even though Democratic candidates won more votes overall — Iowa, Michigan, North Carolina, Pennsylvania and Wisconsin.

The efficiency gap analysis showed more states with a Republican edge than a Democratic one in their U.S. and state House districts.

Yet "when you look at the nation as a whole, it's not just a radically tilted map," said Eric McGhee, a researcher at the nonpartisan Public Policy Institute of California who developed the efficiency gap model. "It's more that in these certain key states, they're paving the way for things to be much worse in the future" through gerrymandering.

The Republican State Leadership Committee, which has mounted an aggressive campaign to elect Republican-led legislatures ahead of redistricting, dismisses the efficiency gap analysis as "a political and intellectual sham" used to try to advance the fortunes of Democrats. The formula assumes a party has a right to win seats based on its statewide vote even if its candidates in particular districts aren't that good, said the group's president, Matt Walter.

"This is not a real formula. This is not a real theory," he said. "This is ivory-tower nonsense."

Republicans' success during the last round of redistricting led top Democrats such as former Obama administration Attorney General Eric Holder to launch a counteroffensive leading up to the 2020 Census. Part of the Democrats' strategy has been to challenge Republican-drawn maps in court and support voter initiatives that shift redistricting duties away from state lawmakers.

Democrats contend their candidates are good but face long odds in districts where the boundaries have been manipulated.

"The value of the efficiency gap is it puts some data behind what we see is evident from election results," said Patrick Rodenbush, communications director for the National Democratic Redistricting Committee. "It's clear that Republicans have an unfair advantaged based fully on gerrymandering."