The winner of the unusually spirited Republican contest was Joseph J. Lhota, a no-nonsense former chairman of the Metropolitan Transportation Authority. He defeated John A. Catsimatidis, a voluble billionaire who ran an often whimsical campaign. 
 
 Mr. Lhota, who served as deputy mayor to Rudolph W. Giuliani, will carry his tough-minded approach to crime-fighting and city spending into the general election on Nov. 5. A vocal supporter of Mayor Michael R. Bloomberg’s policies, Mr. Lhota called his victory “the first step in continuing a strong future for our city.” 
 
 By contrast, Mr. de Blasio’s vow to make a clean break from the Bloomberg era struck a chord with Democratic voters worried about jobs and schools. Roughly three in four wanted to move the city in a different direction after 12 years with Mr. Bloomberg, an exit poll found. 
 
 In the unexpectedly heated race for the unglamorous office of city comptroller, Scott M. Stringer, the Manhattan borough president, repelled a last-minute comeback attempt by Eliot Spitzer, who resigned as governor over his use of prostitutes. And in the Democratic primary for public advocate, Letitia James, a City Council member from Brooklyn, will face Senator Daniel L. Squadron of Brooklyn in a runoff election. 
 
 Photo 
 
 In the race for mayor, Mr. de Blasio had until a few months ago been a distant fourth in a crowded Democratic field, well behind Christine C. Quinn, the longtime front-runner, who rose to prominence as the speaker of the City Council and a close ally of Mr. Bloomberg. Mr. de Blasio, propelled by an unrelenting critique of the mayor, frustrated Ms. Quinn’s painstakingly cultivated effort to become the first woman and the first openly gay person to lead the city. 
 
 Mr. Thompson, who nearly unseated Mr. Bloomberg in 2009, struggled this time to win over nonwhite voters who were crucial to his campaign strategy as the only black candidate in the race. 
 
 Mr. de Blasio, a white Brooklynite who frequently showcased his biracial family, built a broad coalition of support among nearly every category of Democratic primary voters on Tuesday, according to the exit poll by Edison Research. His critique of a city divided between rich and poor — tried in the past by other candidates in New York and nationally with little success — resonated. 
 
 Advertisement Continue reading the main story 
 
 “I love his message about the tale of two cities, the big inequality gap,” said Jelani Wheeler, 19, a politics student at St. John’s University in Queens. 
 
 “We can start correcting many important issues the city is facing, issues often ignored by the Bloomberg administration,” he added. 
 
 Though Democrats outnumber Republicans in New York by six to one, the city has not elected a Democratic mayor since David N. Dinkins in 1989. But the white voters who helped elect Mr. Giuliani and Mr. Bloomberg are a less potent political force in a city where ethnic and racial minorities now make up a majority of the population. 
 
 Mr. de Blasio, who worked for Mr. Dinkins, this year inspired Democrats angry about how the economy and police practices affected them personally, as well as those who felt uncomfortable on their behalf. 
 
 Newsletter Sign Up Continue reading the main story Please verify you're not a robot by clicking the box. Invalid email address. Please re-enter. You must select a newsletter to subscribe to. Sign Up You agree to receive occasional updates and special offers for The New York Times's products and services. Thank you for subscribing. An error has occurred. Please try again later. View all New York Times newsletters. 
 
 He promised to end racial profiling, overhaul the stop-and-frisk practice and replace Police Commissioner Raymond W. Kelly. His critique of the police went further than that of either Ms. Quinn or Mr. Thompson — and he memorably deployed his teenage son, Dante, to make his case in a compelling television advertisement. 
 
 Addressing widespread concerns about the gap between the city’s rich and poor, Mr. de Blasio also proposed a tax on the wealthy to pay for universal prekindergarten — though his rhetoric made it sometimes seem unclear which was the end and which the means. 
 
 His tax-the-rich proposal was dismissed by his rivals, who said it would never be approved in Albany. But at times Ms. Quinn and Mr. Thompson were left in the unwelcome position of sounding like proponents of the status quo, at a moment when voters made clear they were impatient to be led in a different direction. 
 
 Photo 
 
 Mr. de Blasio gained support from black voters like Jennifer Ward, 59, of Brownsville, Brooklyn, who on Tuesday helped her 85-year-old mother, in a flowing white dress with black polka-dots, up the steps of the Brownsville Beacon Community Center and into the big gymnasium. 
 
 “He’s strong and he fights,” said Ms. Ward, a nurse, who was impressed by Mr. de Blasio’s efforts to prevent the closing of local hospitals. “Even if he has to stand alone, he fights for us.” 
 
 Advertisement Continue reading the main story 
 
 The Democrats had been jockeying for position ever since Mr. Bloomberg squeezed out a narrower-than-expected victory over Mr. Thompson four years ago. 
 
 The presumptive favorite a year ago was Ms. Quinn, given her close ties to Mr. Bloomberg and the business community; in the spring, the race shifted with the entry of Anthony D. Weiner, a former congressman, who briefly led the field before sinking over revelations that he had continued sending sexually explicit online messages even after resigning. 
 
 Ms. Quinn’s defeat was crushing for a politician whose career as speaker was built on a belief that helping Mr. Bloomberg carry out his agenda would give her a record of accomplishments to run on. Her crucial role in making Mr. Bloomberg’s third term possible antagonized a huge swath of Democratic voters who showed they were unwilling to forgive and forget. 
 
 Mr. Thompson relied on a campaign strategy built on old-fashioned New York City identity politics and electoral arithmetic. He worked hard to enlist support from establishment political players, while obtaining important municipal union endorsements. 
 
 The police, the firefighters, the transit workers and, most important, the teachers all lined up behind Mr. Thompson; the teachers’ union alone spent about $2.6 million on his behalf. 
 
 Pointing to his record as a behind-the-scenes power broker as school board president and as a fiscal steward while city comptroller, Mr. Thompson positioned himself as the adult in the Democratic field, running well to the right of Mr. de Blasio — and even campaigning with former Senator Alfonse M. D’Amato, once the state Republican kingpin. 
 
 In the event of a runoff, Mr. de Blasio would begin with an fund-raising advantage of about $400,000 over Mr. Thompson. Each would be able to spend a maximum of $3.123 million, under a spending limit imposed by the city’s campaign finance program. 
 
 But on Tuesday night, Mr. de Blasio was celebrating. As guests ate schnitzel with shallots and ginger served on soft pretzel heros, the crowd focused on the now-familiar sight of Dante’s outsized Afro, which Mr. de Blasio’s daughter, Chiara, affectionately caressed. A moment later, when Mr. de Blasio took the stage, he paid his own form of tribute to his son’s hair, telling supporters that Dante had to fend off strangers trying to touch it. 
 
 “Security was ready to intervene,” he said. ||||| Public Advocate Bill de Blasio, who ran an upstart campaign pledging to fight New York City's economic inequality, emerged as the surprising top choice in the Democratic mayoral primary, but could still face weeks _ and another electoral fight _ before becoming his party's nominee. 
 
 Mayoral candidate Bill de Blasio, right, kisses his wife Chirlane McCray after casting his primary vote on Tuesday, Sept. 10, 2013, at the Park Slope Public Library in Brooklyn, N.Y. New Yorkers head... (Associated Press) 
 
 Democratic mayoral hopeful Anthony Weiner holds his son Jordan as he leaves the voting booth after casting his vote at his polling station during the primary election in New York, Tuesday, Sept. 10, 2013.... (Associated Press) 
 
 City Council Speaker and New York City Democratic mayoral hopeful Christine Quinn, right, embraces her wife, Kim Catullo, after Catullo voted in the primary election, Tuesday, Sept. 10, 2013, in New York.... (Associated Press) 
 
 The swirling, chaotic campaign to replace billionaire Mayor Michael Bloomberg, which featured former congressman Anthony Weiner's latest sexting scandal and at least three lead changes in the polls, was fittingly plunged into uncertainty again after the votes were tallied in Tuesday's primary. 
 
 With 97 percent of precincts reporting, de Blasio had about 40.2 percent of the total vote, which puts him a whisker above the 40 percent threshold needed to avoid triggering an automatic Oct. 1 runoff. If he cannot maintain that, he will face former city Comptroller Bill Thompson, who has 26 percent, for a potentially grueling three-week, one-on-one showdown, with the winner advancing to face Republican nominee Joe Lhota, the former chairman of the Metropolitan Transit Authority, in the general election on Nov. 5. 
 
 But it may take a week or more before it is known whether that battle will be fought at all. 
 
 The campaign will take a pause Wednesday as the city stops to observe the anniversary of the Sept. 11, 2001, terrorist attacks. Later this week, election officials will recount all the ballots cast Tuesday. It will likely take until early next week before they tabulate an additional 30,000 or more votes as absentee ballots arrive by mail and paperwork comes in from voters who had problems at the polls. 
 
 City Council Speaker Christine Quinn, the early front-runner who was seeking to become the city's first woman and openly gay mayor, finished third at 16 percent, followed by current city Comptroller John Liu at 7 percent and Weiner at 5 percent. 
 
 De Blasio, who was flanked by the interracial family he made a centerpiece of his campaign ads, made no mention of the potential runoff in his speech to supporters late Tuesday. 
 
 "We are better as a city when we make sure that everyone has a shot," de Blasio told the raucous crowd. "We begin tonight." 
 
 The winner of the mayor's race in November will assume the helm of the nation's largest city which has been led for 12 years by Bloomberg. The election comes at a critical juncture as the city experiences shrinking crime rates yet widening income inequality, and as the nearly completed One World Trade Center building symbolizes a new era after the terrorist attacks of 2001. 
 
 With de Blasio so close to 40 percent, Democratic leaders may pressure Thompson to drop out of the race in the name of party unity. Exit polling shows that de Blasio would handily defeat Thompson in a runoff, 52 to 34 percent, with 9 percent saying they would stay home. 
 
 But Thompson made it clear Tuesday he would compete in a potential runoff. 
 
 "Three more weeks! Three more weeks!" chanted Thompson, the party's 2009 nominee. "This is far from over." 
 
 De Blasio's rise in the race to succeed Bloomberg was as sudden as it was unexpected. 
 
 Not even two months ago, he was an afterthought in the campaign but surged in part thanks to an ad blitz that centered on his interracial family, his headline-grabbing arrest while protesting the possible closure of a Brooklyn hospital and the defection of Weiner's former supporters in the wake of another sexting scandal. 
 
 Weiner was leading in the polls until a gossip website revealed that he used the online handle Carlos Danger to continue to send X-rated messages to women even after he resigned from the House of Representatives in 2011 for similar behavior. 
 
 The exit polling showed the appeal of de Blasio, the city's elected public advocate, to be broad-based: He was ahead in all five boroughs; was ahead of Quinn, the lone woman in the race, with female voters; and ahead of Thompson, the only African-American candidate, with black voters. The voter interviews were conducted by Edison Media Research for The Associated Press and other news organizations. 
 
 Lhota, the ex-transit authority chairman and former deputy mayor to Rudolph Giuliani, defeated billionaire grocery magnate John Catsimatidis for the Republican nomination. Both Republican candidates largely pledged to follow Bloomberg's lead, focusing on maintaining the city's record low crime rates. 
 
 Another scandal-scarred politician, Eliot Spitzer, who resigned as New York's governor in 2008 after paying for sex with prostitutes, tried to run a self-financed campaign for the lesser office of city comptroller. But his distant, television-heavy campaign struggled to connect with voters and he lost to Manhattan Borough President Scott Stringer by a margin of 52 percent to 48 percent. 
 
 Bloomberg, the businessman Republican-turned-independent, is completing his third term. While the city's registered Democrats outnumber Republicans 6 to 1, the Republican Party's recent success in mayoral elections has been largely attributed to a crime epidemic, the aftermath of the Sept. 11 attacks or other extraordinary circumstances. 
 
 Nearly three-quarters of Democratic primary voters said the next mayor ought to move away from Bloomberg policies, according to the exit polls. 
 
 De Blasio, 52, has fashioned himself as the cleanest break from the Bloomberg years, proposing to raise taxes on the wealthy to fund universal pre-kindergarten and change city police practices he says discriminate against minorities. 
 
 De Blasio, who worked in Bill Clinton's White House and Hillary Rodham Clinton's Senate campaign before being elected to the city council and then public advocate, became the front-runner in the race's final weeks. 
 
 Quinn was the front-runner for much of the year, boasting the biggest campaign war chest and strong establishment backing. But she was dogged by her support to change term limits to let Bloomberg run again in 2009, a decision unpopular with liberals who make up the bulk of Democratic primary voters.
– As expected, Bill de Blasio will easily finish first in New York City's Democratic primary for mayor, reports the AP. What's unclear is whether he will get the necessary 40% to avoid a runoff with the second-place finisher. With 97% of returns in, de Blasio had 40.02%, Bill Thompson 26%, and Christine Quinn 15%. Trailing those three were city comptroller John Liu (7%) and, yes, Anthony Weiner (5%). The eventual Democratic winner will face former MTA chief (and Rudy Giuliani deputy) Joe Lhota in the general election in November. He won the GOP race over billionaire John Catsimatidis. There will be a recount of yesterday's ballots, and some 30,000 votes, including absentee ballots, still need to be counted a first time. A final result may not be known for 10 days and Thompson has made it clear that he won't drop out in the name of party unity: He chanted "Three more weeks!" at his election night event, referring to a time before a potential runoff, which polls show he would lose. Exit polls showed that de Blasio, the city's public advocate, did well with a wide swath of voters from different ethnic groups and locales, reports the New York Times. More so than the other candidates, he advocated a clean break from Michael Bloomberg, making a particular issue of Bloomberg's stop-and-frisk policy. The mayor interjected himself into the campaign when he accused de Blasio of running a "racist" campaign—in part because the candidate featured his Afro-sporting teen son in a TV ad. De Blasio is white, his wife is black, and he would be first city mayor with an interracial family, notes the Times.