[Question]
  [
# Introduction
Write a solver for the [Hitori](https://en.wikipedia.org/wiki/Hitori) puzzles using least bytes.
# Challenge
Your task is write a solver for the Hitori (ひとり, the word for "alone" in Japanese; the meaning of the game name is "Leave me alone") logical puzzles. The rules are as follows:
* You are presented with a n-by-n grid of cells, each cell contains an integer between 1 and n (inclusive).
* Your goal is to make sure that no number appear more than once in each row and each column of the grid, by removing numbers from the given grid, subject to the restriction indicated in the next two rules,
* You cannot remove two numbers from two adjacent (horizontally or vertically) cells.
* The remaining numbered cells must be all connected to each other. It means that any two remaining numbered cells can be connected with a curve that is composed solely of segments connecting adjacent remaining numbers (horizonally or vertically). (Thanks to @user202729 for pointing out that this is missing)
I hope the rules are clear by now. If there is anything unclear about the rules, check the [Wikipedia page](https://en.wikipedia.org/wiki/Hitori). 
# Test Cases
The cells from which the numbers are removed are represented with 0s.
```
Input  ->  Output
4
2 2 2 4      0 2 0 4
1 4 2 3  ->  1 4 2 3
2 3 2 1      2 3 0 1
3 4 1 2      3 0 1 2
4
4 2 4 3      0 2 4 3
4 1 1 2  ->  4 1 0 2
3 1 2 1      3 0 2 1
4 3 1 3      0 3 1 0
5
1 5 3 1 2      1 5 3 0 2
5 4 1 3 4      5 0 1 3 4
3 4 3 1 5  ->  3 4 0 1 5
4 4 2 3 3      4 0 2 0 3
2 1 5 4 4      2 1 5 4 0
8
4 8 1 6 3 2 5 7      0 8 0 6 3 2 0 7
3 6 7 2 1 6 5 4      3 6 7 2 1 0 5 4
2 3 4 8 2 8 6 1      0 3 4 0 2 8 6 1
4 1 6 5 7 7 3 5  ->  4 1 0 5 7 0 3 0
7 2 3 1 8 5 1 2      7 0 3 0 8 5 1 2
3 5 6 7 3 1 8 4      0 5 6 7 0 1 8 0
6 4 2 3 5 4 7 8      6 0 2 3 5 4 7 8
8 7 1 4 2 3 5 6      8 7 1 4 0 3 0 6
9
8 6 5 6 8 1 2 2 9      8 0 5 6 0 1 2 0 9
5 6 2 4 1 7 9 8 3      5 6 2 4 1 7 9 8 3
5 8 2 5 9 9 8 2 6      0 8 0 5 0 9 0 2 0
9 5 6 6 4 3 8 4 1      9 5 6 0 4 3 8 0 1
1 1 6 3 9 9 5 6 2  ->  0 1 0 3 9 0 5 6 2
1 1 4 7 3 8 3 8 6      1 0 4 7 0 8 3 0 6
3 7 4 1 2 6 4 5 5      3 7 0 1 2 6 4 5 0
3 3 1 9 8 7 7 4 5      0 3 1 9 8 0 7 4 5
2 9 7 5 3 5 9 1 3      2 9 7 0 3 5 0 1 0 
```
These test cases are taken from [Concept Is Puzzles](http://www.conceptispuzzles.com/index.aspx?uri=puzzle/euid/010000009ca896ef2fb6a5a4b63d1dd96e51c0295f72a24561f98409baf064ecfb79f6bba827f62665d86d96d267ca4f6bc6bf07/play), [PuzzleBooks](http://www.puzzlebooks.net/images/puzzles/solutions/070871f80140c9de6463dcb95c66c8a4.png), [Concept Is Puzzles](http://www.conceptispuzzles.com/index.aspx?uri=puzzle/hitori/rules), [Wikipedia](https://en.wikipedia.org/wiki/Hitori), and [Youtube](https://www.youtube.com/watch?v=iaAdKqT2TVo), respectively.
# Specs
* No need to worry about exception handling.
* You **can** assume that the input is always a valid puzzle with a **unique** solution and you can take advantage of this in writing your code.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), the lowest number of bytes wins.
* 4 <= n <= 9 (16 originally, changed to 9 following Stewie Griffin's suggestion, also save some trouble in IO)
* You can take input and provide output through [any standard form](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods), and you are free to choose the format.
* Some suggestions for the output format are (but you are not restricted to these)
	+ Outputting the final grid
	+ Outputting the grid containing all removed numbers
	+ Output a list of coordinates of one of the above
* As usual, [default loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) apply here.
---
Related(inspired by this challenge): [Check if All Elements In a Matrix Are Connected](https://codegolf.stackexchange.com/questions/154363/check-if-all-elements-in-a-matrix-are-connected)
My last challenge: [Extension of the Game of Sevens](https://codegolf.stackexchange.com/questions/154147/extension-of-the-game-of-sevens)
  
[Answer]
# [Haskell](https://www.haskell.org/), 374 bytes
```
import Data.Array;import Data.List;r=range;p=partition
c(e,f)=p(\(b,p)->any(==1)[(b-d)^2+(p-q)^2|(d,q)<-e])f
n#g=[s|(o,(e:f))<-[p((==0).(g!))$indices g],
 null.fst$c(o,o),null.snd$until(null.fst)c([e],f),
 s<-case[((l,c),d)|((l,c),h)<-assocs g,h>0,
 d<-[filter((==h).(g!))$r((l,c+1),(l,n))++r((l+1,c),(n,c))],d/=[]]
 of[]->[g];((c,d):_)->n#(g//[(c,0)])++n#(g//[(c,0)|c<-d])]
```
[Try it online!](https://tio.run/##bVHbbtswDH33V3hIHkiETmLn4jiJAwzYy4D9gaYVrm8R6squ5DwUyL9nlJIWBTYQpMTDc0haPhf2pe662029Dr0Zwx/FWMy/G1O8H74iv5QdDyY3hW7rw5APhRnVqHodlFBTg/kAv@GZBoxOhX6HPI9RwHNU4Z9kBkP0xucVKnrDY1RLbAI9aXNhr9AT1PsGGRYDsGyJc2i/IU6VrlRZ27CVFIT60nXzxo7TkgU9ks@trqYXPaoOPspYgqglb8MSe4zKwtYCoKMSqcLr43bmWYW1fcm96XxaMrfi6Y3qxtq4Fc4fKxivmMVIfGrE2cwhs9h1Ac0RJVWLXEgZhH0jZHQSrTwAlDxt/8QvoSfQLhaCgSVKln/Nr@UxqiTKWxSFMMH9/qceo5N/dwC@EzuiB8V/UBm8Fkrng1F6nGql66DpLyZfTzr@Tw9@TG71Na0RRULO1hSzJ7R6eMzRYYkMXI88@1eeUcbyHW1pw75zZLbMZ/eGKWc7brThmHDMfJ5w/c7aMmvFiOM623KWfXaI/UqpZzh31fS@lFdu2FZsse@a@trGb5D6ipvHnyFvfwE "Haskell – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 133 bytes[SBCS](https://github.com/abrudz/SBCS)
```
{q←{⊢/4 2⍴⍵}⌺3 3⋄g←⍵=⊂∪,⍵⋄⍵×~1⊃{((⌈/q b)⌈b<{2<≢∪0,,(⍵×⊢⌈⌈/∘q)⍣≡⍵×(⍴⍵)⍴1+⍳≢,⍵}¨~b∘⌈¨⊂⍤2∘.≡⍨⍳⍴b)(+/↑w<g×⌈.⌈⍨w×g)⌈w b←⍵}⍣≡×\(⌈/=∘⌽⍨q⍵)0}
```
[Try it online!](https://tio.run/##RVDNbhMxEL7zFHvbDQ3JeOyxvSh9Ey6JaKKKKCFRpQhF2wOgKg3ZCg4oHCHlEHElXJC4NG/iFwmfvZUq7/pn5vuz@2/HL16/64@no1O4@3o5DTef6TTEvJzFKax3XZNxqA@h/lOFzV@d6fDp4wg9FM7D@n1Y/WpjiyLm4/ZahfWHZVGEzao7ywYtrIPeknvhdgcktdtFgkEXnQgKq2@zVqjvw@2P1CkaL5QO6izUv0GM@tXD/noALCgP@2hb/2QcO4m2j7j6MGgVZ91w82XRG8Fhs@pEi3q/OG5HMcciGzSxq8buuH2VYp4n2X9AzqIxVafTVbz7BFNHnqPeEejhAuma/ewiyjxGWO84m2STFPoOyRLkAuA5QMOs/zIf9i/HOUjZvED9@7wFQD59k1fPrnJmNsqwxlDaKCamxzMpfJwDY9igpVgrVkajnM4ECCvSiiJGCbpioIGhxEACiljRIBZKDUIBBqmRWMYrq1mcto6VFZCMZ2@VwcE5LQ6eXuAs1mFnLBBinPcuZhRLnsCnhk8QhUfDJ3GkKf6RT@CTApie@GjZmMFbsV4xl2LxFK70WjxLWXq2KFmjPe6OmGUEKGWc9tpb7fBY1ohorUrvnBEunWgplfZww8vRkyBiCpWUSgRBUhi6pChIxhEQEKQkSBQFqREkjZej/D8 "APL (Dyalog Unicode) – Try It Online")
My implementation of rule #4 (cells must form a single connected component) is rather wasteful, but still this passes all tests in about 10 seconds on TIO.
---
The overall algorithm: Store two boolean matrices `b` and `w` for cells that are certain to be black and white respectively. Initialise `b` as all-zero. Initialise `w` as 1 only for those cells that have opposite matching neighbours.
Repeat until `b` and `w` settle down:
* add to `b` cells that are on the same line (horizontal or vertical) and of the same value as a cell in `w`
* add to `w` the immediate neighbours of all cells in `b`
* add to `w` all cutpoints - cells whose removal would split the graph of non-black cells into multiple connected components
Finally, output `not(b)` multiplied by the original matrix.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 62 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
Uses [user202729's isConnected monadic link](https://codegolf.stackexchange.com/a/154447/53748) from another question.
---
```
FJṁa@µ«Ḋoµ€ZUµ4¡ÐLFQL<3
ḟ0ĠḊ€
¬T€œ&2\;Ç€FȦ
ZÇȯÇ_1Ŀ
2ḶṗLṗLa⁸ÇÞḢ
```
A full program printing a representation of a list of lists.  
Works by brute force and is stupidly inefficient.
**[Try it online!](https://tio.run/##y0rNyan8/9/N6@HOxkSHQ1sPrX64oyv/0NZHTWuiQg9tNTm08PAEH7dAHxtjroc75hscWQCUBspxHVoTAqSOTlYzirE@3A5kup1YxhV1uP3E@sPt8YZH9nMZPdyx7eHO6T4gnPioccfh9sPzHu5Y9P///@hoQx0FIyCK1VGINgIzjUFMYx0FkERsLAA "Jelly – Try It Online")** - a 3 by 3, since it is too inefficient to run even a size 4 within the 60 second TIO limit!
### How?
```
FJṁa@µ«Ḋoµ€ZUµ4¡ÐLFQL<3 - Link 1 isConnected? List of lists
...                     - 1 if connected 0 if not -- see linked answer in the header
ḟ0ĠḊ€ - Link 2, helperFor-AnyRepeatedValues: list
ḟ0    - filter out zeros
  Ġ   - group indices by value (i.e. [[indices of min],...,[indices of max]]
   Ḋ€ - dequeue €ach -- leaving a list of empty lists iff no repeated values
      -                 any remaining values are non-zero (1-based indexing in Jelly)
¬T€œ&2\;Ç€FȦ - Link 3, columnwiseAnyAdjacentZerosOrRowwiseAnyRepeatedValues: list of lists
¬            - logical not (convert all zeros to ones and all others to zeros)
 T€          - for €ach row get a list of truthy indexes (i.e. indexes of original zeros)
     2\      - pairwise reduction (i.e. for neighbouring rows) with:
   œ&        -   intersection (empty if no columnwise adjacent original zeros
             -                 any remaining values are non-zero due to 1-based indexing)
        Ç€   - call last link (1) as a monad for €ach row
       ;     - concatenate
          F  - flatten into a single list (empty iff no columnwise adjacent original zeros
             -                                   AND no rowwise repeated values)
           Ȧ - any and all (0 if empty [or contains any zero -- never] else 1)
ZÇȯÇ_1Ŀ - Link 4, validity check? list of lists
Z       - transpose
 Ç      - call last link (2) as a monad rowwiseAnyAdjacentZerosOrColumnwiseAnyRepeatedValues?
   Ç    - call last link (2) as a monad columnwiseAnyAdjacentZerosOrRowwiseAnyRepeatedValues?
  ȯ     - logical OR
     1Ŀ - call link 1 as a monad (isConnected?)
    _   - subtract
        - this yields -1 for valid, while it yields 0 or 1 if not.
2ḶṗLṗLa⁸ÇÞḢ - Main link: list of lists
2Ḷ          - lowered range of 2 -> [0,1]
   L        - length (number of rows in the input)
  ṗ         - Cartesian power (all lists of zeros and ones of length L)
     L      - length (number of rows in the input again)
    ṗ       - Cartesian power (all grids of zeros and ones of same shape as the input)
       ⁸    - the input
      a     - logical AND -- effectively uses each of the formed grids as a mask
         Þ  - sort by:
        Ç   -   last link (3) as a monad
          Ḣ - head
            - implicit print
```
[Answer]
# Python3, 567 bytes:
```
E=enumerate
M=[(0,1),(0,-1),(1,0),(-1,0)]
V=lambda b:[(x,r,y,v)for x,r in E(b)for y,v in E(r)]
J=list.count
def C(b,x,y,s):
 b=eval(str(b));b[x][y]=0
 if b in s:return
 t=[(x,y)for x,_,y,v in V(b)if v]
 q=[t.pop()]
 while q:
  x,y=q.pop(0)
  for X,Y in M:
   if(N:=(x+X,y+Y))in t:q+=[N];t.remove(N)
 if[]==t:return b
def f(b):
 q,s=[b],[]
 while q:
  b=q.pop(0)    
  F=1
  for x,r,y,v in V(b):
   if v and(J(r,v)>1 or J([*[*zip(*b)][y]],v)>1):
    F=0
    if all(b[x+X][y+Y]for X,Y in M if 0<=x+X<len(b)and 0<=y+Y<len(r))and(B:=C(b,x,y,s)):q+=[B];s+=[B]
  if F:return b
```
[Try it online!](https://tio.run/##bVHBbuIwED3XXzE3bHBRQkBahbqHrtoDUjlWIG9UxcVRI4UkOIYl@/PsjMmyu1IVyeN589688aTt/WdTJ99ad7k8K1sf99bl3rJXpXkkYyHxvKcQywjPewoZe1NVvje7HEyq@Vk62cuTKBoHeIeyhmduQorwNXUoWqmq7Pz0oznWnu1sAd@5kWeUdiJlYJQ95RXvvEOtWBp9znSfqYhBWYChLl3qrD@6moFX5NoPju9ysHlDJZJPGYOD0n7aNi1HX/j5WVYWDmiC7F4dQiESmFKDjdyS@JXK6MXXqeLnyUb2k60QWPDpYaL0Olv6qbP75mT5WtBQOlPKDyOBCQ8qcABsc5Cd0iaT@n9vc3O@w@xFxcMAw/7@vGCYA06Q1zu@4g5X@xgDEldcj/X4V9nysRG0nSyUrgpsGIWI0ryqOC5wskHSZJv9@0oqRw8Kaw@VrdEOTQhAXgCcIIQ/perv3xFhA0/ZsguBBY@X29MvXQwKRqMRmwF9cxbDHGOCeYIxZgnmMcwYcbrZQCbKHElUomJCJ5IRw1tyJScDOYZFgGdsEXphx9CVsAVKgl9wJCbmQU7/xDfvpsndjmPETd0NU2s93uctL2svoZx2bVV6LkRYVUmLKsrKW8fXTW0loHSgjH7UI6Qx1jqU8oLf2nexEOILePY1nCB8@Q0)
]