question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
number-of-closed-islands
Easy to understand DFS solution with COMMENTS
easy-to-understand-dfs-solution-with-com-uxbm
Yo, since I spent some time on this question, just want to help you guy save time.\n\n\nclass Solution {\n boolean dfs(int [][] grid, int i, int j) {\n
nhatdinh041
NORMAL
2019-11-10T08:08:20.689247+00:00
2019-11-10T08:21:22.742249+00:00
537
false
Yo, since I spent some time on this question, just want to help you guy save time.\n\n```\nclass Solution {\n boolean dfs(int [][] grid, int i, int j) {\n int row = grid.length;\n int col = grid[0].length;\n \n // go beyond of the border is not a good idea\n if (i < 0 || j < 0 || i...
6
0
['Depth-First Search', 'Java']
2
number-of-closed-islands
c++ bfs/dfs without vis array approach
c-bfsdfs-without-vis-array-approach-by-z-aiea
Intuition\n Describe your first thoughts on how to solve this problem. \nbfs dfs \nthe code is well explained\n//also i had a third approach whichi knew but i
zaidokhla
NORMAL
2023-04-06T20:24:05.807362+00:00
2023-04-06T20:24:05.807476+00:00
1,658
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nbfs dfs \nthe code is well explained\n//also i had a third approach whichi knew but i dont why it is not working on leetcode \nin that we basically we ran dfs from the boundary cell and mark islands from there as 1 \nthen we iterated ove...
5
0
['C++']
2
number-of-closed-islands
Striver type approch :)
striver-type-approch-by-piyush512002-divy
Intuition\nFirst traverse through boundary just like in surrounded region question then we need to traverse in island just like number of island type question b
piyush512002
NORMAL
2023-04-06T10:17:09.378347+00:00
2023-05-13T18:24:48.583861+00:00
323
false
# Intuition\nFirst traverse through boundary just like in surrounded region question then we need to traverse in island just like number of island type question basically its a mixture of surrounded region + number od island question.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexit...
5
0
['Depth-First Search', 'C++', 'Java']
1
number-of-closed-islands
JAVA || BEATS 100% || EASY UNDERSTANDING || SIMPLE || DFS || BIT MANIPULATION
java-beats-100-easy-understanding-simple-3k7w
Code\n\nclass Solution {\n public int closedIsland(int[][] grid) {\n int cnt=0;\n int n=grid.length,m=grid[0].length;\n boolean vis[][]=
shaikhu3421
NORMAL
2023-04-06T05:08:11.853084+00:00
2023-04-06T05:12:18.678912+00:00
549
false
# Code\n```\nclass Solution {\n public int closedIsland(int[][] grid) {\n int cnt=0;\n int n=grid.length,m=grid[0].length;\n boolean vis[][]=new boolean[n][m];\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==0){\n cnt+=dfs(i,j,...
5
1
['Bit Manipulation', 'Depth-First Search', 'Java']
4
number-of-closed-islands
Easy & Clear Solution Python 3 DFS
easy-clear-solution-python-3-dfs-by-moaz-htmx
\n# Code\n\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n def dfs(i,j,newval):\n if grid[i][j]==0:\n
moazmar
NORMAL
2023-04-06T04:01:01.222455+00:00
2023-04-06T04:01:01.222488+00:00
574
false
\n# Code\n```\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n def dfs(i,j,newval):\n if grid[i][j]==0:\n grid[i][j]=2\n if i<len(grid)-1 : dfs(i+1,j,newval)\n if i >0 : dfs(i-1,j,newval)\n if j<len(grid[0])-1 : d...
5
0
['Depth-First Search', 'Python3']
1
number-of-closed-islands
Python DFS, BFS, Recursive, Iterative, DSU
python-dfs-bfs-recursive-iterative-dsu-b-av6k
DFS \n## recursive\nDeal with the borders first (we ignore them) and then proceed to inner islands\npython\nclass Solution:\n def closedIsland(self, grid: Li
nth-attempt
NORMAL
2022-12-29T05:07:41.480444+00:00
2022-12-29T05:07:41.480491+00:00
671
false
# DFS \n## recursive\nDeal with the borders first (we ignore them) and then proceed to inner islands\n```python\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n R, C = len(grid), len(grid[0])\n directions = [(1,0), (0,1), (-1,0), (0,-1)]\n \n def is_valid(r, c):\...
5
0
['Depth-First Search', 'Breadth-First Search', 'Union Find', 'Python', 'Python3']
0
number-of-closed-islands
1254. Number of Closed Islands Using DFS
1254-number-of-closed-islands-using-dfs-aoga7
Similar Problem\n 130. Surrounded Regions\n# Code \n\nclass Solution {\npublic:\n void dfs(int i,int j,vector<vector<int>>& grid,vector<vector<int>>&v
Pallavi_Dhakne
NORMAL
2022-12-27T09:15:28.936393+00:00
2022-12-27T09:15:28.936433+00:00
1,059
false
# Similar Problem\n 130. Surrounded Regions\n# Code \n```\nclass Solution {\npublic:\n void dfs(int i,int j,vector<vector<int>>& grid,vector<vector<int>>&vis,int n,int m,int drow[],int dcol[])\n {\n vis[i][j]=1;\n for(int k=0;k<4;k++)\n {\n int nrow=drow[k]+i;\n int nc...
5
0
['Array', 'Depth-First Search', 'Breadth-First Search', 'C++']
2
number-of-closed-islands
Java DFS || 2ms || Beats 96.01%🔥
java-dfs-2ms-beats-9601-by-anujpanchal20-90pn
To solve this problem efficiently, we first need to understand how this problem is different from 2000. Number of Islands.\nThe difference is that we have to ex
anujpanchal2001
NORMAL
2022-11-19T14:32:16.617827+00:00
2022-11-19T14:34:57.394353+00:00
844
false
To solve this problem efficiently, we first need to understand how this problem is different from [2000. Number of Islands](https://leetcode.com/problems/number-of-islands/?envType=study-plan&id=graph-i).\nThe difference is that we have to exclude the islands that have a land cell at the top, left, right or bottom bord...
5
0
['Depth-First Search', 'Java']
0
number-of-closed-islands
Java | DFS | explanation
java-dfs-explanation-by-f096t004-8udx
Closed islands is the islands which a island or multiple connected-islands closed by water 4-directionally. Hence, if you look the image below, then you\'ll fi
f096t004
NORMAL
2022-06-21T17:46:52.957533+00:00
2022-06-21T17:46:52.957582+00:00
658
false
Closed islands is the islands which a island or multiple connected-islands closed by water **4-directionally**. Hence, if you look the image below, then you\'ll find out that the islands on the right-top side is not defined as closed islands. That\'s because it is not totally surrendered by the water. The rest side of...
5
0
['Depth-First Search', 'Java']
0
number-of-closed-islands
[C++] dfs + UnionFind ( disjoint set )
c-dfs-unionfind-disjoint-set-by-kormulev-q4gv
Sure it is far from the optimal solution but just one of the possible implementatoin.\nThe idea is that first of all we invert all the islands that are not cons
kormulev
NORMAL
2021-08-19T14:31:41.357857+00:00
2021-08-19T14:31:41.357905+00:00
504
false
Sure it is far from the optimal solution but just one of the possible implementatoin.\nThe idea is that first of all we invert all the islands that are not considered closed, i.e. not enclosed with water. It means that such islands are on borders. By inversion i mean that we flood them with water. After that we are onl...
5
0
['Depth-First Search', 'Union Find', 'C']
0
number-of-closed-islands
Well explained Python solution with DFS
well-explained-python-solution-with-dfs-u2xn0
Number of closed Islands\nhttps://leetcode.com/problems/number-of-closed-islands/submissions/\n\nclass Solution(object):\n def closedIsland(self, grid):\n
pakilamak
NORMAL
2020-08-19T03:11:26.685801+00:00
2020-09-21T01:44:52.001553+00:00
849
false
**Number of closed Islands**\nhttps://leetcode.com/problems/number-of-closed-islands/submissions/\n```\nclass Solution(object):\n def closedIsland(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: int\n """\n count =0\n \n # iterate through the grid from 1 to...
5
0
['Python']
0
number-of-closed-islands
Java. BFS. Island search.
java-bfs-island-search-by-red_planet-77kk
\n\nclass Solution {\n public int isIsland(int []ij, int[][] grid)\n {\n boolean yes = true;\n int [][]moves = new int[][]{{1,0}, {-1,0}, {0
red_planet
NORMAL
2023-04-06T20:03:29.103064+00:00
2023-04-06T20:03:29.103097+00:00
308
false
\n```\nclass Solution {\n public int isIsland(int []ij, int[][] grid)\n {\n boolean yes = true;\n int [][]moves = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};\n LinkedList<int[]> island = new LinkedList<>();\n island.add(ij);\n grid[ij[0]][ij[1]] = 2;\n int []cur;\n ...
4
0
['Java']
0
number-of-closed-islands
JAVA || 100% || easy hai re yeh soln dekh le samjh jayega
java-100-easy-hai-re-yeh-soln-dekh-le-sa-yhgn
Intuition \n Describe your first thoughts on how to solve this problem. \n1. dekh bahi har ek index pe iterate karna hai\n2. if 0 aaya toh check karenge agar is
ankitmaurya-
NORMAL
2023-04-06T12:22:35.524623+00:00
2023-04-06T12:22:35.524666+00:00
151
false
# Intuition \n<!-- Describe your first thoughts on how to solve this problem. -->\n1. dekh bahi har ek index pe iterate karna hai\n2. if 0 aaya toh check karenge agar iska network end me(0 ,grid.length-1) join hota hai toh toh bhai wo island nahi hai\n3. aur jidhar jidhar jayega mark chor ke janeka\n\n# Approach\n<!-- ...
4
0
['Java']
1
number-of-closed-islands
c++ easy solution using bfs method
c-easy-solution-using-bfs-method-by-pooj-085u
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nBFS method\n\n# Complexity\n- Time complexity:\nNear to O(nm)\n\n- Space
pooja_9154
NORMAL
2023-04-06T10:44:54.260379+00:00
2023-04-06T10:46:17.716390+00:00
537
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nBFS method\n\n# Complexity\n- Time complexity:\nNear to O(nm)\n\n- Space complexity:\nO(nm)+space taken by queue(O(n))\n\n# Code\n```\nclass Solution {\npublic:\nvoid bfs(int x,int y,vector<vector<int>>&vis,vector<vector<int...
4
0
['Breadth-First Search', 'Queue', 'C++']
3
number-of-closed-islands
Two Unique Solutions Using DFS and Union Find
two-unique-solutions-using-dfs-and-union-hmfe
Readable Names and Self Explanatory if both concepts known.\n# Code Using DFS\n\nclass Solution {\npublic:\n vector<vector<bool>>visited;\n vector<int>add
Heisenberg2003
NORMAL
2023-04-06T05:35:53.596853+00:00
2023-04-06T05:39:19.298129+00:00
1,650
false
Readable Names and Self Explanatory if both concepts known.\n# Code Using DFS\n```\nclass Solution {\npublic:\n vector<vector<bool>>visited;\n vector<int>adderx={0,0,1,-1};\n vector<int>addery={1,-1,0,0};\n void explore_area(vector<vector<int>>&grid,int i,int j)\n {\n for(int k=0;k<4;k++)\n ...
4
1
['Depth-First Search', 'Union Find', 'Matrix', 'C++']
0
number-of-closed-islands
Easy C++ Solution || USING DFS
easy-c-solution-using-dfs-by-lakshya_12-2vtz
Intuition\n Describe your first thoughts on how to solve this problem. \nUpvote if its help!!\n\n# Approach\n Describe your approach to solving the problem. \nS
lakshya_12
NORMAL
2023-04-06T03:40:45.402505+00:00
2023-04-06T03:40:45.402562+00:00
850
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUpvote if its help!!\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSolving This question by Depth First Search (DFS)\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n-...
4
0
['Depth-First Search', 'Graph', 'C++']
0
number-of-closed-islands
C++ || Beats 89% || simple brute force Explained💯
c-beats-89-simple-brute-force-explained-3vqh8
\n# Approach\n Describe your approach to solving the problem. \n- Make a vis2DArray initialise it with 0\n- Find cells having 0 (land) in the grid from two Loop
wasim_khan0101
NORMAL
2022-12-30T09:34:36.428696+00:00
2022-12-30T09:34:36.428732+00:00
608
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Make a **vis2DArray** initialise it with **0**\n- Find cells having 0 (land) in the grid from two Loops\n- Call dfs for that cell having 0 in the grid\n- **Check 4-direc if 0 is at the boundary return false After marking that piece of land in the...
4
0
['C++']
3
number-of-closed-islands
Clean C++ solution using DFS with faster than 94.43% of C++ || With easy explanation of code
clean-c-solution-using-dfs-with-faster-t-k0ds
Logic\n\t1. First of all visit all the lands which are in boundary using DFS , i.e take a single 1 from the corner and stretch the spread in all 4 directions s
_D_I_V_A_K_A_R
NORMAL
2022-06-07T01:42:07.692490+00:00
2022-07-27T17:51:54.899294+00:00
254
false
**Logic**\n\t1. First of all ***visit all the lands which are in boundary using DFS*** , i.e take a single 1 from the corner and stretch the spread in **all 4 directions** so as to cover the boundaries and parallely **make\nthem visited** .\n\t2. As now we have covered all the islands which are in the corners, now our...
4
0
['Depth-First Search', 'C']
0
number-of-closed-islands
c++ || easy to understand || DFS
c-easy-to-understand-dfs-by-drishti1210-oxxl
\nclass Solution {\npublic:\n int n;\n int m;\n \n bool waterPresent(vector<vector<int>>& grid, int i, int j){\n if(grid[i][j])\n
drishti1210
NORMAL
2022-05-22T12:39:55.250061+00:00
2022-05-22T12:39:55.250094+00:00
218
false
```\nclass Solution {\npublic:\n int n;\n int m;\n \n bool waterPresent(vector<vector<int>>& grid, int i, int j){\n if(grid[i][j])\n return true;\n if(i <= 0 || i >= n-1 || j <= 0 || j >= m-1)\n\t\t\treturn false;\n grid[i][j] = 1;\n\t\tbool l = waterPresent(grid, i, j-1);\n\t...
4
1
['Depth-First Search', 'Graph', 'C', 'C++']
0
number-of-closed-islands
Java Easy Self Expalantory
java-easy-self-expalantory-by-bharat194-s62p
\nclass Solution {\n public int closedIsland(int[][] grid) {\n int res = 0;\n for(int i=0;i<grid.length;i++){\n if(grid[i][0] == 0)
bharat194
NORMAL
2022-03-25T05:17:59.469532+00:00
2022-03-25T05:17:59.469569+00:00
201
false
```\nclass Solution {\n public int closedIsland(int[][] grid) {\n int res = 0;\n for(int i=0;i<grid.length;i++){\n if(grid[i][0] == 0) dfs(grid,i,0);\n if(grid[i][grid[0].length-1] == 0) dfs(grid,i,grid[0].length-1);\n }\n for(int i=0;i<grid[0].length;i++){\n ...
4
0
['Java']
0
number-of-closed-islands
Simple c++ soution
simple-c-soution-by-amartya_k-zlpv
\nint closedIsland(vector<vector<int>>& grid) {\n int count=0;\n bool flag;\n \n for(int i=1;i<grid.size()-1;i++){\n \n
amartya_k
NORMAL
2022-03-19T05:47:40.077241+00:00
2022-03-19T05:47:40.077287+00:00
258
false
```\nint closedIsland(vector<vector<int>>& grid) {\n int count=0;\n bool flag;\n \n for(int i=1;i<grid.size()-1;i++){\n \n for(int j=1;j<grid[0].size()-1;j++){\n if(grid[i][j]==0){\n flag=false;\n dfs(grid,i,j,flag);\...
4
0
['Depth-First Search', 'C', 'C++']
1
number-of-closed-islands
[Python3] DFS - logic operator - easy undestanding
python3-dfs-logic-operator-easy-undestan-puhw
\ndef closedIsland(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n closed_island = 0\n \n def dfs(row: int,
dolong2110
NORMAL
2021-12-22T17:12:49.293628+00:00
2021-12-22T17:12:49.293659+00:00
123
false
```\ndef closedIsland(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n closed_island = 0\n \n def dfs(row: int, col: int) -> bool:\n if row < 0 or row >= m or col < 0 or col >= n:\n return False\n \n if grid[row][col] == ...
4
1
['Depth-First Search', 'Recursion', 'Python', 'Python3']
0
number-of-closed-islands
C++ | DFS on boundary | Approach Explained
c-dfs-on-boundary-approach-explained-by-hpk78
Approach-We apply dfs on boundary and mark all boundary Os and its connected components as -1(to recognize they are visited). Now,the matrix consists of only th
nidhi_ranjan
NORMAL
2021-10-01T18:24:45.828308+00:00
2021-10-01T18:26:06.752343+00:00
412
false
Approach-We apply dfs on boundary and mark all boundary Os and its connected components as -1(to recognize they are visited). Now,the matrix consists of only those Os that are surrounded by water.So, we perform dfs on the matrix and find the number of connected components.\n```\nclass Solution {\npublic:\n void dfs(...
4
0
['Depth-First Search', 'Recursion', 'C', 'C++']
0
number-of-closed-islands
Java simple BFS
java-simple-bfs-by-hobiter-ii09
\nclass Solution {\n int[][] g;\n int m, n;\n boolean[][] vs;\n int[][] dir = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n public int closedI
hobiter
NORMAL
2020-06-29T03:06:05.865244+00:00
2020-06-29T03:06:05.865276+00:00
561
false
```\nclass Solution {\n int[][] g;\n int m, n;\n boolean[][] vs;\n int[][] dir = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n public int closedIsland(int[][] grid) {\n g = grid;\n m = g.length; \n n = g[0].length;\n vs = new boolean[m][n];\n int res = 0;\n fo...
4
0
[]
0
number-of-closed-islands
Java, DFS, Union Find, explained
java-dfs-union-find-explained-by-gthor10-1x2r
In this problem we can use Union-Find structure which is usually faster then dfs. THe only catch w need to take care of is these land cells on the edge.\nFor ed
gthor10
NORMAL
2019-11-11T20:32:22.968193+00:00
2019-11-11T20:37:21.996264+00:00
722
false
In this problem we can use Union-Find structure which is usually faster then dfs. THe only catch w need to take care of is these land cells on the edge.\nFor edge land I scan 0-th and last row and 0 and last column and mark land cell as water. For every land cell do dfs cause it could be a whole island connected to the...
4
0
['Depth-First Search', 'Union Find', 'Java']
1
number-of-closed-islands
Simple DFS Solution with comments
simple-dfs-solution-with-comments-by-man-e241
We do a simple dfs for the 0s which are at the boundary of the grid and mark them by changing their value to -2.\nNow we call dfs using the function func in the
manrajsingh007
NORMAL
2019-11-10T04:04:55.165381+00:00
2019-11-10T04:34:15.719335+00:00
524
false
We do a simple dfs for the 0s which are at the boundary of the grid and mark them by changing their value to -2.\nNow we call dfs using the function func in the code to get the number of islands which are surrounded by water on all sides. While doing this we also mark the cells as visited by making cell value as -1.\n`...
4
3
['Depth-First Search', 'Java']
2
number-of-closed-islands
💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
easiestfaster-lesser-cpython3javacpython-q30y
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n Describe your first thoughts on how to solve this problem. \n- J
Edwards310
NORMAL
2024-11-29T13:08:52.262868+00:00
2024-11-29T13:08:52.262902+00:00
134
false
![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/9fc46acb-7ba4-42e1-864c-3b0e7e0e82b6_1730795144.4340796.jpeg)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n- ***JavaScript Code -->**...
3
0
['Depth-First Search', 'C', 'Matrix', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']
0
number-of-closed-islands
Detailed Solution for beginners 🤗🤗🤗🤗
detailed-solution-for-beginners-by-utkar-hgpi
Welcome, Keep working hard and thank you for visiting \uD83E\uDD17\uD83E\uDD17\n\nNow, on to the solution \u23ED\uFE0F\n# Approach\n 1. Mark Water at the Border
utkarshpriyadarshi5026
NORMAL
2024-05-08T18:56:23.538966+00:00
2024-05-08T18:56:23.538995+00:00
34
false
# Welcome, Keep working hard and thank you for visiting \uD83E\uDD17\uD83E\uDD17\n\nNow, on to the solution \u23ED\uFE0F\n# Approach\n 1. **Mark Water at the Borders**\nFirst, we need to ensure that any 0s connected to the grid\'s border are marked as water (`1`), because these are not part of any closed island.\n\n ...
3
0
['Depth-First Search', 'Graph', 'Matrix', 'Java']
0
number-of-closed-islands
Easy Union Find Approach
easy-union-find-approach-by-anupsingh556-gxod
Intuition\nCreate Union of all connected water bodies and land bodies then remove those land bodies which are connected with borders.\n\n\n# Code\n\nclass Solut
anupsingh556
NORMAL
2024-03-26T18:00:33.887369+00:00
2024-03-26T18:00:33.887401+00:00
7
false
# Intuition\nCreate Union of all connected water bodies and land bodies then remove those land bodies which are connected with borders.\n\n\n# Code\n```\nclass Solution {\npublic:\n vector<int> p;\n int findP(int a){\n if(p[a]==a)return a;\n return findP(p[a]);\n }\n\n void merge(int a, int b)...
3
0
['C++']
0
number-of-closed-islands
✅Java Solution | 🚀2ms runtime | 🔥Depth First Search | 💚Beginner Friendly
java-solution-2ms-runtime-depth-first-se-lvtd
Approach\nTraverse the array and\n- First do Depth-First Search with elements 0 (i.e. land ) as boundarys and make them 1 as they are not our concern.\n- Secon
5p7Ro0t
NORMAL
2023-04-09T19:09:05.088991+00:00
2023-04-09T19:09:31.649419+00:00
200
false
# Approach\nTraverse the array and\n- First do Depth-First Search with elements `0` (i.e. land ) as boundarys and make them `1` as they are not our concern.\n- Second do Depth-First Search on the remaining lands (`0`) that are present, they are not connected with boundary.\n\nand count each time we call dfs, so that m...
3
0
['Depth-First Search', 'Matrix', 'Java']
2
number-of-closed-islands
✅C++ Solution || Simple DFS || Queue(Iterative) || Explained and commented
c-solution-simple-dfs-queueiterative-exp-s4g3
\n\n# Code\n\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int r=grid.size(),c=grid[0].size();\n int ans=0;\n
Devanshul
NORMAL
2023-04-06T20:06:03.537589+00:00
2023-04-06T20:06:33.422961+00:00
60
false
![WhatsApp Image 2023-04-07 at 01.34.17.jpg](https://assets.leetcode.com/users/images/fc94eeb7-6463-412e-b465-ba421e10791b_1680811477.6485174.jpeg)\n\n# Code\n```\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int r=grid.size(),c=grid[0].size();\n int ans=0;\n int d...
3
0
['Graph', 'Queue', 'C++']
0
number-of-closed-islands
Explained approach and Easy solution in Java, C++ and JavaScript (beats 91%)
explained-approach-and-easy-solution-in-ug686
Intuition\nThe function closedIsland takes a 2D grid as input and returns the number of closed islands in the grid. The function first initializes some variable
Aryan_rajput_
NORMAL
2023-04-06T17:57:53.392342+00:00
2023-04-06T17:57:53.392374+00:00
75
false
# Intuition\nThe function closedIsland takes a 2D grid as input and returns the number of closed islands in the grid. The function first initializes some variables: rows and cols to store the dimensions of the grid, and closedIslands to store the count of closed islands.\n\nThe function then defines a helper function t...
3
0
['Array', 'Depth-First Search', 'C++', 'Java', 'JavaScript']
0
number-of-closed-islands
Simple dfs solution!!
simple-dfs-solution-by-venkataakhil4518-ikae
Code\n\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n vis=defaultdict(lambda:False)\n n=len(grid)\n m=len(gr
venkataakhil4518
NORMAL
2023-04-06T17:28:04.104699+00:00
2023-04-06T17:28:04.104726+00:00
1,197
false
# Code\n```\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n vis=defaultdict(lambda:False)\n n=len(grid)\n m=len(grid[0])\n \'\'\'\n \'\'\'\n def dfs(x,y):\n vis[(x,y)]=True\n isedge=True\n for i,j in ((x+1,y),(x-1,y...
3
0
['Python', 'Python3']
1
number-of-closed-islands
DSU || C++ || Explained
dsu-c-explained-by-mikerufy-6hv3
\n# Approach\n Describe your approach to solving the problem. \n\n\n\n\nPlease upvote :)\n\nIf its possible without using a set please share\n# Code\n\nclass So
mikerufy
NORMAL
2023-04-06T17:17:46.581032+00:00
2023-04-06T17:17:46.581067+00:00
127
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n![image.png](https://assets.leetcode.com/users/images/f5573a5e-edef-4c32-94f2-347e807adcab_1680801368.0498996.png)\n\n![image.png](https://assets.leetcode.com/users/images/d0f1e8c4-c79e-42d2-827b-40cd1c03da85_1680801385.6659582.png)\n\nPlease upvote...
3
0
['Union Find', 'C++']
1
number-of-closed-islands
Easy DFS solution 🔥 | In-depth explanation | various methods discussed (C++)
easy-dfs-solution-in-depth-explanation-v-vagn
Intuition\nIts a variation of a basic dfs problem where we need to count the number of islands (ie, group of 0s). But doing that here would result in overcounti
moinak878
NORMAL
2023-04-06T14:49:04.274178+00:00
2023-04-06T14:49:04.274221+00:00
114
false
# Intuition\nIts a variation of a basic dfs problem where we need to count the number of islands (ie, group of `0`s). But doing that here would result in overcounting. `How do we solve this problem ?`\n\n# Approach\nThere can be two methods :-\n1) We can encorporate the boundary check condition in the dfs itself where...
3
0
['C++']
0
number-of-closed-islands
✅Java||Full Explaination🔥||Comments||lBeginner Friendly🔥
javafull-explainationcommentslbeginner-f-mx22
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst will make those land(0s) as water(1s) which are present in the boundary and simul
deepVashisth
NORMAL
2023-04-06T11:28:22.866976+00:00
2023-04-06T11:39:08.432010+00:00
218
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst will make those land(0s) as water(1s) which are present in the boundary and simultanouelsy will make all land(0s) as water(1s) inside the boundary which are connected to boundary.\nThe reason why we are making all land(0s) as water(...
3
0
['Matrix', 'Java']
2
number-of-closed-islands
Optimal Depth-First Search Solution for Counting Closed Islands in a 2D Grid
optimal-depth-first-search-solution-for-hkzb1
\n\n# Approach\nIn this solution, we iterate over all the cells in the grid and check if it is an unvisited 0. If it is, we perform a depth-first search (DFS) o
priyanshu11_
NORMAL
2023-04-06T11:26:39.833434+00:00
2023-04-06T11:26:39.833465+00:00
73
false
\n\n# Approach\nIn this solution, we iterate over all the cells in the grid and check if it is an unvisited 0. If it is, we perform a depth-first search (DFS) on the island to check if it is closed.\n\nThe DFS function dfs takes in the grid, the current cell\'s coordinates, and the dimensions of the grid. It returns a ...
3
0
['Array', 'Depth-First Search', 'Matrix', 'C++', 'Java']
0
number-of-closed-islands
Python Elegant & Short | DFS
python-elegant-short-dfs-by-kyrylo-ktl-jrhp
Complexity\n- Time complexity: O(nm)\n- Space complexity: O(nm)\n\n# Code\n\nclass Solution:\n LAND = 0\n WATER = 1\n\n def closedIsland(self, grid: Li
Kyrylo-Ktl
NORMAL
2023-04-06T07:42:17.337531+00:00
2023-04-06T07:42:34.020201+00:00
498
false
# Complexity\n- Time complexity: $$O(n*m)$$\n- Space complexity: $$O(n*m)$$\n\n# Code\n```\nclass Solution:\n LAND = 0\n WATER = 1\n\n def closedIsland(self, grid: List[List[int]]) -> int:\n n, m = len(grid), len(grid[0])\n\n for row in range(n):\n self.water_island(row, 0, grid)\n...
3
0
['Depth-First Search', 'Python', 'Python3']
0
number-of-closed-islands
BFS SOLUTION || C++ SOLUTION || SAME AS NUMBER OF ISLANDS BUT WITH SOME EXTRA BOUNDARY CONDITIONS
bfs-solution-c-solution-same-as-number-o-42ck
\nSame as number of island problem but with some boundary conditions.\nUsed bfs for finding out the number of island.\n\n\nclass Solution {\npublic:\nbool take
_chintu_bhai
NORMAL
2023-04-06T07:30:47.491923+00:00
2023-04-06T07:31:12.284635+00:00
375
false
\nSame as number of island problem but with some boundary conditions.\nUsed bfs for finding out the number of island.\n\n```\nclass Solution {\npublic:\nbool take;\nvoid bfs(int i,int j,int trow,int tcol,vector<vector<int>>&vis,vector<vector<int>>& grid)\n{\n queue<pair<int,int>>q;\n q.push({i,j});\n while(!q...
3
0
['Breadth-First Search', 'Graph', 'C++']
0
number-of-closed-islands
Java BFS solution
java-bfs-solution-by-darshan_behere-rwkd
Intuition\n Describe your first thoughts on how to solve this problem. \nFind all the island and if it touches border skip that island.\n\n# Approach\n Describe
Darshan_Behere
NORMAL
2023-04-06T04:53:27.751608+00:00
2023-04-06T04:53:27.751653+00:00
229
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind all the island and if it touches border skip that island.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. BFS to explore the grid\n2. If the grid[i][j] comes in the contact of border then flag=false\n3. If ...
3
0
['Java']
1
number-of-closed-islands
✍ ✅ [PHP🚀 27ms Beats 100%][PHP][JavaScript 96.55%] Depth-First-Search Approach
php-27ms-beats-100phpjavascript-9655-dep-fhfy
\n# Approach\nThis approach uses depth-first-search to traverse through the grid and count the number of closed islands in the grid. \n\nWe check each cell of t
akunopaka
NORMAL
2023-04-06T03:11:38.266089+00:00
2023-04-06T03:11:38.266115+00:00
256
false
\n# Approach\nThis approach uses depth-first-search to traverse through the grid and count the number of closed islands in the grid. \n\nWe check each cell of the grid, if it is 0 (meaning land), then we perform a depth-first-search to check if it is a closed island or not. If it is a closed island, we increment the co...
3
0
['Depth-First Search', 'PHP', 'JavaScript']
1
number-of-closed-islands
Easiest Solution
easiest-solution-by-code_ranvir25-gjid
Complexity\n- Time complexity:\nO(nm)\n\n- Space complexity:\nO(nm)\n\n# Code\n\nclass Solution {\n public int closedIsland(int[][] grid) {\n // Defin
cOde_Ranvir25
NORMAL
2023-04-06T02:52:02.541020+00:00
2023-04-06T02:52:02.541066+00:00
215
false
# Complexity\n- Time complexity:\nO(n*m)\n\n- Space complexity:\nO(n*m)\n\n# Code\n```\nclass Solution {\n public int closedIsland(int[][] grid) {\n // Define the 4 directions to explore (up, down, left, right)\n int dirs[][]={{-1,0},{1,0},{0,-1},{0,1}};\n // Get the number of rows and columns i...
3
0
['Breadth-First Search', 'Queue', 'Java']
0
number-of-closed-islands
𝐒𝐢𝐦𝐩𝐥𝐞 𝐃𝐅𝐒 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧✅✅
simple-dfs-solution-by-deleted_user-av3d
\n# Code\n\nclass Solution {\npublic:\n void DFS(vector<vector<int>>& grid,int i,int j,int& val)\n {\n if(i<0 || i==grid.size() || j<0 || j==grid[0
deleted_user
NORMAL
2023-04-06T01:03:19.531009+00:00
2023-04-06T01:03:19.531041+00:00
262
false
\n# Code\n```\nclass Solution {\npublic:\n void DFS(vector<vector<int>>& grid,int i,int j,int& val)\n {\n if(i<0 || i==grid.size() || j<0 || j==grid[0].size())\n {\n val=0;\n return;\n }\n if(grid[i][j]!=0)return ;\n grid[i][j]=1;\n DFS(grid,i+1,j,va...
3
0
['C++']
0
number-of-closed-islands
Java | DFS | Clean code | Beats > 82%
java-dfs-clean-code-beats-82-by-judgemen-7pxj
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
judgementdey
NORMAL
2023-04-06T00:43:57.486497+00:00
2023-04-06T00:45:43.432173+00:00
33
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m*n)$$ on the stack\n<!-- Add your spac...
3
0
['Depth-First Search', 'Recursion', 'Matrix', 'Java']
0
number-of-closed-islands
C++ DFS traversal
c-dfs-traversal-by-khatung-jmtd
Intuition\n Describe your first thoughts on how to solve this problem. \nGraph Traversal.\n# Approach\n Describe your approach to solving the problem. \nIterate
khatung
NORMAL
2023-02-17T06:38:29.137442+00:00
2023-04-06T09:19:22.183650+00:00
743
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGraph Traversal.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterate the grid and have a counter of island. If reach a land cell, perform a DFS traversal from that cell. \nFor each traversal, everytime we find a ...
3
1
['C++']
2
number-of-closed-islands
Union Find
union-find-by-jiaqiong-tm26
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Jiaqiong
NORMAL
2023-02-15T00:00:30.680845+00:00
2023-02-15T00:00:30.680876+00:00
132
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['Union Find', 'C++']
0
number-of-closed-islands
C++ || DFS || Easy approach
c-dfs-easy-approach-by-mrigank_2003-4fhg
Here is my c++ code for this problem.\nDFS:-\n\'\'\'\n\n\tclass Solution {\n\tpublic:\n\t\tbool dfs(int x, int y, vector>& grid){\n\t\t\tif(x<0 || x>=grid.size(
mrigank_2003
NORMAL
2022-11-18T13:59:59.771094+00:00
2022-11-18T13:59:59.771128+00:00
1,246
false
Here is my c++ code for this problem.\nDFS:-\n\'\'\'\n\n\tclass Solution {\n\tpublic:\n\t\tbool dfs(int x, int y, vector<vector<int>>& grid){\n\t\t\tif(x<0 || x>=grid.size() || y<0 || y>=grid[0].size()){return false;}\n\t\t\tif(grid[x][y]==1){return true;}\n\t\t\tgrid[x][y]=1;\n\t\t\tbool chk1=dfs(x-1, y, grid), chk2=d...
3
0
['Depth-First Search', 'C']
1
number-of-closed-islands
C++ Well Explained BFS solution
c-well-explained-bfs-solution-by-singhal-yfia
\nclass Solution {\n //direction pair array\n pair<int, int> direction[4] = {{1,0}, {-1,0}, {0,1}, {0, -1}};\npublic:\n bool Bfs(int i, int j, vector<v
singhalPratham
NORMAL
2022-11-01T20:06:19.817012+00:00
2022-11-01T20:06:19.817044+00:00
663
false
```\nclass Solution {\n //direction pair array\n pair<int, int> direction[4] = {{1,0}, {-1,0}, {0,1}, {0, -1}};\npublic:\n bool Bfs(int i, int j, vector<vector<int>>& grid, int n, int m){\n queue<pair<int, int>> q1;\n q1.push({i, j});\n bool res = true;\n while(!q1.empty()){\n ...
3
0
['Breadth-First Search', 'Graph', 'C']
0
number-of-closed-islands
C++ | DFS | Comments Added | Easy approach
c-dfs-comments-added-easy-approach-by-sh-du80
\nclass Solution {\npublic:\nint row;\nint col;\n void dfs(int i, int j, vector<vector<int>> &grid)\n {\n if(i < 0 || j < 0 || j >= col || i >= row
shreyanshxyz
NORMAL
2022-07-15T19:52:00.360792+00:00
2022-07-15T19:52:00.360827+00:00
79
false
```\nclass Solution {\npublic:\nint row;\nint col;\n void dfs(int i, int j, vector<vector<int>> &grid)\n {\n if(i < 0 || j < 0 || j >= col || i >= row || grid[i][j] != 0) return;\n// (3) now if we find a piece of land connected to the border we change that 1 into 2, because its not of any use\n ...
3
0
['Depth-First Search', 'C']
0
number-of-closed-islands
✅ Python, Easy to understand DFS solution, 95.50% runtime
python-easy-to-understand-dfs-solution-9-7drp
\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n result = 0\n \n def dfs(grid, r, c):\n if not 0 <
AntonBelski
NORMAL
2022-06-28T15:18:31.717096+00:00
2022-06-28T15:18:54.050600+00:00
393
false
```\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n result = 0\n \n def dfs(grid, r, c):\n if not 0 <= r < len(grid) or not 0 <= c < len(grid[0]):\n return False\n if grid[r][c] != 0:\n return True\n \n ...
3
0
['Depth-First Search', 'Python3']
2
number-of-closed-islands
C++|| DFS|| Beginner Friendly || Commented Solution || Number of island approach
c-dfs-beginner-friendly-commented-soluti-r48j
class Solution {\npublic:\n \n //Function to submerge all island that are at boundary of grid\n void change (vector > &grid, int n, int m, int i, int j
ritik_pr3003
NORMAL
2022-03-27T05:37:55.304604+00:00
2022-03-27T05:37:55.304629+00:00
235
false
class Solution {\npublic:\n \n //Function to submerge all island that are at boundary of grid\n void change (vector <vector <int>> &grid, int n, int m, int i, int j){\n if(i<0 || j<0 || i>=n || j>=m || grid[i][j]==1){\n return ;\n }\n grid[i][j]=1;\n change(grid,n,m,i+1,j...
3
0
['Depth-First Search', 'C', 'C++']
0
number-of-closed-islands
Easy dfs
easy-dfs-by-breaker250611-xv8k
\n//Simply traversed through the grid and return the min of dfs applying if it is on the side corners that will give you 0 and if not that will give you 1 and j
breaker250611
NORMAL
2022-03-23T09:29:27.670390+00:00
2022-03-23T09:30:02.175359+00:00
89
false
```\n//Simply traversed through the grid and return the min of dfs applying if it is on the side corners that will give you 0 and if not that will give you 1 and just added all them\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int count = 0 ;\n \n for(int i=0;i<gri...
3
0
[]
1
number-of-closed-islands
Easy C++ solution || DFS || with well explained comments
easy-c-solution-dfs-with-well-explained-oii0l
``` \nvoid dfs(vector>& grid,int i,int j){\n if(i<0||i>=grid.size()||j<0||j>=grid[0].size()||grid[i][j]==1) //checking boundary conditions\n
Harsh_Soni_0734
NORMAL
2022-03-04T05:50:57.413154+00:00
2022-03-04T05:56:57.760941+00:00
123
false
``` \nvoid dfs(vector<vector<int>>& grid,int i,int j){\n if(i<0||i>=grid.size()||j<0||j>=grid[0].size()||grid[i][j]==1) //checking boundary conditions\n return;\n grid[i][j]=1;\n dfs(grid,i+1,j); //traversing in downward direction\n dfs(grid,i-1,j); //traversing in upward directi...
3
0
['Depth-First Search', 'Recursion']
0
number-of-closed-islands
Python 3 Solution | DFS | O(1) Space
python-3-solution-dfs-o1-space-by-1nnoce-9rtc
py\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n n, m = len(grid), len(grid[0])\n def dfs(i, j):\n if i
1nnOcent
NORMAL
2022-03-03T11:02:11.384332+00:00
2022-03-03T11:02:11.384363+00:00
292
false
```py\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n n, m = len(grid), len(grid[0])\n def dfs(i, j):\n if i == n or j == m or i < 0 or j < 0 or grid[i][j]:\n return\n \n grid[i][j] = 1\n dfs(i+1, j)\n dfs(...
3
0
['Depth-First Search', 'Python']
0
number-of-closed-islands
Java easy dfs
java-easy-dfs-by-ankitkumarmahato-d5y7
\nclass Solution {\n private boolean solve(int row,int col,int[][] grid,boolean[][] vis){\n if(row<0 || row>=grid.length || col<0 || col>=grid[0].leng
ankitkumarmahato
NORMAL
2022-02-15T18:23:16.913624+00:00
2022-02-15T18:23:16.913655+00:00
176
false
```\nclass Solution {\n private boolean solve(int row,int col,int[][] grid,boolean[][] vis){\n if(row<0 || row>=grid.length || col<0 || col>=grid[0].length)\n return false;\n if(grid[row][col]==1 || vis[row][col])\n return true;\n \n vis[row][col]=true;\n bool...
3
0
['Java']
1
n-repeated-element-in-size-2n-array
[Java/C++/Python] O(1) Solution
javacpython-o1-solution-by-lee215-mhu6
Solution 1\nUse array or set and return seen number at once.\nO(N) time, O(N) space\n\nJava, use array\n\n public int repeatedNTimes(int[] A) {\n int[
lee215
NORMAL
2018-12-23T18:50:11.233107+00:00
2020-02-01T03:34:46.761858+00:00
25,113
false
### Solution 1\nUse array or set and return seen number at once.\n`O(N)` time, `O(N)` space\n\n**Java, use array**\n```\n public int repeatedNTimes(int[] A) {\n int[] count = new int[10000];\n for (int a : A)\n if (count[a]++ == 1)\n return a;\n return -1;\n }\n```\n...
214
26
[]
44
n-repeated-element-in-size-2n-array
C++ 2 lines O(4) | O (1)
c-2-lines-o4-o-1-by-votrubac-tv68
The intuition here is that the repeated numbers have to appear either next to each other (A[i] == A[i + 1]), or alternated (A[i] == A[i + 2]).\n\nThe only excep
votrubac
NORMAL
2018-12-23T04:01:53.958833+00:00
2018-12-23T04:01:53.958878+00:00
10,302
false
The intuition here is that the repeated numbers have to appear either next to each other (```A[i] == A[i + 1]```), or alternated (```A[i] == A[i + 2]```).\n\nThe only exception is sequences like ```[2, 1, 3, 2]```. In this case, the result is the last number, so we just return it in the end. This solution has O(n) runt...
139
5
[]
12
n-repeated-element-in-size-2n-array
Circular array O(n) time and O(1) Space
circular-array-on-time-and-o1-space-by-d-35cd
If a number is repeated N times in a list of size 2N, it is always possible for the repeated number to stay within a distance of 2.\nConsider this exaple where
destinynitsed
NORMAL
2018-12-23T04:16:29.918477+00:00
2018-12-23T04:16:29.918547+00:00
3,352
false
If a number is repeated N times in a list of size 2N, it is always possible for the repeated number to stay within a distance of 2.\nConsider this exaple where N = 4, Number **x** is repeated twice. All possible comibnations for x to fit in a list of size 4 are:\n[a,b,x,x]\n[x,a,b,x] (distance between both the x is sti...
43
3
[]
6
n-repeated-element-in-size-2n-array
Python one-liner beats 100%
python-one-liner-beats-100-by-darktianti-r69n
\n def repeatedNTimes(self, A):\n """\n :type A: List[int]\n :rtype: int\n """\n return int((sum(A)-sum(set(A))) // (len(A
darktiantian
NORMAL
2018-12-23T06:19:20.058786+00:00
2018-12-23T06:19:20.058849+00:00
5,336
false
```\n def repeatedNTimes(self, A):\n """\n :type A: List[int]\n :rtype: int\n """\n return int((sum(A)-sum(set(A))) // (len(A)//2-1))\n```
41
2
[]
13
n-repeated-element-in-size-2n-array
PYTHON 3 : SUPER EASY 99.52% FASTER
python-3-super-easy-9952-faster-by-rohit-be5b
184 ms, faster than 99.52% USING LIST\n\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n \n list1 = []\n for i in
rohitkhairnar
NORMAL
2021-07-14T15:07:19.010505+00:00
2021-07-14T15:27:30.110490+00:00
2,398
false
**184 ms, faster than 99.52%** USING LIST\n```\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n \n list1 = []\n for i in nums :\n if i in list1 :\n return i\n else :\n list1.append(i)\n```\n**192 ms, faster than 95.77%** U...
22
0
['Ordered Set', 'Python', 'Python3']
1
n-repeated-element-in-size-2n-array
[2 methods] [1st using hashmap] [2nd using sorting][c++]
2-methods-1st-using-hashmap-2nd-using-so-lakm
1.Using hashmap TC:o(n)\n\n class Solution {\n public:\n int repeatedNTimes(vector& A) {\n unordered_map map;\n for(int i=0;i
rajat_gupta_
NORMAL
2020-08-25T17:36:57.178588+00:00
2020-10-03T13:51:11.363812+00:00
1,730
false
**1.Using hashmap TC:o(n)**\n\n class Solution {\n public:\n int repeatedNTimes(vector<int>& A) {\n unordered_map<int,int> map;\n for(int i=0;i<A.size();i++){\n if(map[A[i]]>0)\n return A[i];\n map[A[i]]++;\n } \n ...
18
0
['C', 'Sorting', 'C++']
2
n-repeated-element-in-size-2n-array
python 3 easy to understand
python-3-easy-to-understand-by-akaghosti-yok1
\tclass Solution:\n\t\tdef repeatedNTimes(self, A: List[int]) -> int:\n\t\t\tB = set(A)\n\t\t\treturn (sum(A) - sum(B)) // (len(A) - len(B))
akaghosting
NORMAL
2019-07-13T18:18:44.460952+00:00
2019-07-13T18:18:44.460995+00:00
1,943
false
\tclass Solution:\n\t\tdef repeatedNTimes(self, A: List[int]) -> int:\n\t\t\tB = set(A)\n\t\t\treturn (sum(A) - sum(B)) // (len(A) - len(B))
16
0
[]
5
n-repeated-element-in-size-2n-array
3 Different Approaches
3-different-approaches-by-sahil20000706-a70a
Approach 1: Using Hash Map\nWe can use hash map to store the frequency of each element and then from hash map we can return the element with frequency == size/2
sahil20000706
NORMAL
2021-09-14T05:15:10.659232+00:00
2022-03-14T10:22:56.608388+00:00
956
false
## **Approach 1: Using Hash Map**\nWe can use hash map to store the frequency of each element and then from hash map we can return the element with frequency == size/2.\n\nTime - O(n)\nSpace - O(n)\n\n\n**Code:-**\n\n```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& nums) {\n unordered_map<int,...
14
0
['C', 'C++']
3
n-repeated-element-in-size-2n-array
JS - Faster than 97% - Using Sets
js-faster-than-97-using-sets-by-nilsonmo-9snl
\nvar repeatedNTimes = function(A) {\n let lookup = new Set();\n\n for (let n of A) {\n if (lookup.has(n)) return n;\n lookup.add(n);\n }\n\n return -
nilsonmolina
NORMAL
2019-05-31T19:24:01.061169+00:00
2019-05-31T19:24:57.215189+00:00
1,450
false
```\nvar repeatedNTimes = function(A) {\n let lookup = new Set();\n\n for (let n of A) {\n if (lookup.has(n)) return n;\n lookup.add(n);\n }\n\n return -1;\n};\n```
12
0
['JavaScript']
5
n-repeated-element-in-size-2n-array
JAVA: 1 liner, O(n), 4ms, HashSet
java-1-liner-on-4ms-hashset-by-idkwho-t914
\nclass Solution {\n public int repeatedNTimes(int[] A) {\n Set<Integer> store = new HashSet<>();\n\t\tfor(int a: A) if(store.add(a) == false) return
idkwho
NORMAL
2019-02-10T23:18:20.400317+00:00
2019-02-10T23:18:20.400420+00:00
1,821
false
```\nclass Solution {\n public int repeatedNTimes(int[] A) {\n Set<Integer> store = new HashSet<>();\n\t\tfor(int a: A) if(store.add(a) == false) return a;\n return 0;\n }\n}\n```
12
3
[]
3
n-repeated-element-in-size-2n-array
Java 0MS Set
java-0ms-set-by-trevor-akshay-w4um
```\nclass Solution {\n public int repeatedNTimes(int[] A) {\n Set set = new HashSet<>();\n for(int num : A) {\n if(set.contains(num
trevor-akshay
NORMAL
2021-02-16T10:06:54.006234+00:00
2021-02-16T10:06:54.006278+00:00
1,163
false
```\nclass Solution {\n public int repeatedNTimes(int[] A) {\n Set<Integer> set = new HashSet<>();\n for(int num : A) {\n if(set.contains(num)) return num;\n else set.add(num);\n }\n return -1;\n }\n}
11
0
['Ordered Set', 'Java']
1
n-repeated-element-in-size-2n-array
Python beats 100 %
python-beats-100-by-ashishbisht723-b14n
\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n """\n :type :A List[int]\n :rtype int\n """\n d = {}\
ashishbisht723
NORMAL
2019-08-18T16:49:10.862595+00:00
2019-08-18T16:49:10.862625+00:00
1,000
false
```\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n """\n :type :A List[int]\n :rtype int\n """\n d = {}\n for num in A:\n if num in d.keys():\n return num\n else:\n d[num] = 1\n ```
11
0
[]
4
n-repeated-element-in-size-2n-array
C solution
c-solution-by-zhaoyaqiong-f7mo
\u57282N\u7684\u4F4D\u7F6E\u91CC\u4E00\u5171\u6709N+1\u4E2A\u4E0D\u540C\u7684\u5143\u7D20\uFF0C\u88AB\u67E5\u627E\u5143\u7D20\u51FA\u73B0N\u6B21\uFF0C\u6240\u4E
zhaoyaqiong
NORMAL
2018-12-27T06:24:30.563577+00:00
2018-12-27T06:24:30.563639+00:00
1,152
false
\u57282N\u7684\u4F4D\u7F6E\u91CC\u4E00\u5171\u6709N+1\u4E2A\u4E0D\u540C\u7684\u5143\u7D20\uFF0C\u88AB\u67E5\u627E\u5143\u7D20\u51FA\u73B0N\u6B21\uFF0C\u6240\u4EE5\u9664\u4E86\u88AB\u67E5\u627E\u5143\u7D20\uFF0C\u5176\u4ED6\u5143\u7D20\u5747\u53EA\u51FA\u73B0\u4E00\u6B21\u3002\u8003\u8651\u5143\u7D20\u7684\u968F\u673A\u...
11
0
[]
4
n-repeated-element-in-size-2n-array
By sorting :)
by-sorting-by-theadarsh1m-xs9e
\n\n> # Code lelocode\nThe Adarsh 1M <-- Youtube\n\nclass Solution {\n public int repeatedNTimes(int[] nums) {\n Arrays.sort(nums);\n // int fl
TheAdarsh1M
NORMAL
2024-05-06T15:14:26.657503+00:00
2024-05-06T15:14:26.657540+00:00
251
false
\n\n> # Code lelo`code`\n**The Adarsh 1M <-- Youtube**\n```\nclass Solution {\n public int repeatedNTimes(int[] nums) {\n Arrays.sort(nums);\n // int flag = 0;\n for (int i = 1; i < nums.length; ++i){\n int temp = nums[i];\n int tempo = nums[i-1];\n if (temp == t...
6
0
['Java']
4
n-repeated-element-in-size-2n-array
2 📌Fastest Java☕ solutions using HashMap & HashSet 1ms💯💯
2-fastest-java-solutions-using-hashmap-h-s3la
1st approach using HashMap:-\n\nclass Solution {\n public int repeatedNTimes(int[] nums) \n {\n HashMap<Integer,Integer> hmap=new HashMap<>();\n
saurabh_173
NORMAL
2022-05-02T17:12:48.787090+00:00
2022-05-02T17:15:14.124582+00:00
579
false
**1st approach using HashMap:-**\n```\nclass Solution {\n public int repeatedNTimes(int[] nums) \n {\n HashMap<Integer,Integer> hmap=new HashMap<>();\n for(int num:nums)\n {\n hmap.put(num,hmap.getOrDefault(num,0)+1);\n if(hmap.get(num)>1)\n return num;\n ...
6
0
['Hash Table', 'Java']
0
n-repeated-element-in-size-2n-array
Python 3 The Simplest Code!
python-3-the-simplest-code-by-tot0-fc65
\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n return mode(A)\n\n
tot0
NORMAL
2020-05-27T20:47:56.149934+00:00
2020-05-27T20:47:56.149987+00:00
583
false
```\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n return mode(A)\n```\n
6
0
[]
1
n-repeated-element-in-size-2n-array
Python 1 liner using mode
python-1-liner-using-mode-by-lokeshsk1-88zj
\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n return mode(A)\n
lokeshsk1
NORMAL
2020-04-11T14:40:04.256468+00:00
2020-06-27T07:44:48.443584+00:00
565
false
```\nclass Solution:\n def repeatedNTimes(self, A: List[int]) -> int:\n return mode(A)\n```
6
1
['Python', 'Python3']
1
n-repeated-element-in-size-2n-array
100/100 Solution zero additional memory
100100-solution-zero-additional-memory-b-qrl4
In an array of size 2N with N+1 unique elements, the maximum distance between the repeated numbers is 4, so only 4 numbers need to checked at once, and no need
cyberpirate92
NORMAL
2019-12-19T12:06:27.363434+00:00
2019-12-22T08:19:42.774783+00:00
564
false
In an array of size `2N` with `N+1` unique elements, the maximum distance between the repeated numbers is `4`, so only `4` numbers need to checked at once, and no need for additional memory.\n\n```java\npublic int repeatedNTimes(int[] A) {\n\tfor (int i=0; i<=A.length-4; i++) {\n\t\tif (A[i] == A[i+1] || A[i] == A[i+2]...
6
0
['Java']
3
n-repeated-element-in-size-2n-array
Improvement of 2nd official solution
improvement-of-2nd-official-solution-by-iedbv
From the official Approach 2\nThus, we only have to compare elements with their neighbors that are distance 1, 2, or 3 away.\nFor cases with where 2 * N > 4 the
jaltair
NORMAL
2019-12-01T22:40:37.715424+00:00
2019-12-01T22:40:37.715473+00:00
933
false
From the official Approach 2\n`Thus, we only have to compare elements with their neighbors that are distance 1, 2, or 3 away.`\nFor cases with where 2 * N > 4 there is no need to check neighbors that are 3 distance away. \n\n2 * N elements\nx is repeated N times\n\nFor 2 * N == 4 there is one corner case, where distanc...
6
0
[]
2
n-repeated-element-in-size-2n-array
Simple Python Solution
simple-python-solution-by-dodda_sai_sach-iak5
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Dodda_Sai_Sachin
NORMAL
2024-02-21T18:35:43.973407+00:00
2024-02-21T18:35:43.973435+00:00
312
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
5
0
['Python3']
0
n-repeated-element-in-size-2n-array
200 ms Solution beats 85 % in runtime only three lines Deadly simple !
200-ms-solution-beats-85-in-runtime-only-epf5
PLease UPVOTE\n# Code\n\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n freq=(max(nums)+1)*[0]\n for i in range(len(nums
ayan_101
NORMAL
2023-08-06T11:35:54.326218+00:00
2023-08-06T11:35:54.326252+00:00
394
false
PLease UPVOTE\n# Code\n```\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n freq=(max(nums)+1)*[0]\n for i in range(len(nums)):freq[nums[i]]+=1\n return freq.index(max(freq))\n```
5
0
['Python3']
1
n-repeated-element-in-size-2n-array
Simple Solution || 100% faster O(n/2) || JAVA
simple-solution-100-faster-on2-java-by-i-xf31
Approach\nnums.length = 2 * n and exactly one element of nums is repeated n times. It means no one element except we searching cannot appear more than 1 time. I
im_obid
NORMAL
2023-04-13T09:48:34.972562+00:00
2023-04-13T09:48:34.972601+00:00
1,010
false
# Approach\n```nums.length = 2 * n``` and exactly one element of ```nums``` is repeated ```n``` times. It means no one element except we searching cannot appear more than 1 time. If any element of ```nums``` appears two or more times then it is the element that is repeated n times.\n \n\n---\n\n\nI used ```HashSet``` a...
4
0
['Java']
2
n-repeated-element-in-size-2n-array
Java || 3 approaches || brute --> optimise -> 100%
java-3-approaches-brute-optimise-100-by-yg2ku
\n\n public int repeatedNTimes(int[] nums) {\n //Using sort ----> 6ms\n Arrays.sort(nums);\n int number = 0;\n int count = 1;\n
shivambhilarkar
NORMAL
2021-06-24T04:51:34.666145+00:00
2021-06-24T04:51:34.666178+00:00
447
false
``\n\n public int repeatedNTimes(int[] nums) {\n //Using sort ----> 6ms\n Arrays.sort(nums);\n int number = 0;\n int count = 1;\n for(int i = 1; i< nums.length; i++){\n if(nums[i-1] == nums[i]){\n count++;\n }\n if(count == nums.leng...
4
0
['Java']
0
n-repeated-element-in-size-2n-array
[Python Explained] Simple one-liner Counter
python-explained-simple-one-liner-counte-3r9h
Function Counter(nums) gives us the collection of elements along with the number of count\nHere\'s an example:\n\nnums = [2,1,2,5,3,2]\nCounter(nums)\n\nResult\
akashadhikari
NORMAL
2021-05-15T09:16:51.581050+00:00
2021-05-15T09:17:21.938539+00:00
147
false
Function `Counter(nums)` gives us the collection of elements along with the number of count\nHere\'s an example:\n```\nnums = [2,1,2,5,3,2]\nCounter(nums)\n```\nResult\n`Counter({2: 3, 1: 1, 5: 1, 3: 1})` \n\nNow, `Counter(nums).most_common()` returns us the collection of tuples enclosed by a list\nResult\n`[(2, 3), (...
4
0
['Python', 'Python3']
1
n-repeated-element-in-size-2n-array
Java | Simple solution | 100% faster
java-simple-solution-100-faster-by-jeson-buib
The idea behind this implemenation is to return the "first duplicate element encountered"\ni.e Array will surely contains unique elements except the one to be
jesonshawn
NORMAL
2020-11-22T07:01:03.766707+00:00
2020-11-22T08:08:17.026298+00:00
151
false
The idea behind this implemenation is to return the "first duplicate element encountered"\ni.e Array will surely contains unique elements except the one to be returned.\n\nPS : Please upvote if u like this implementation or if u have any feedback, please feel free to drop a note\n\n```\nclass Solution {\n public in...
4
0
[]
0
n-repeated-element-in-size-2n-array
Javascript O(n)
javascript-on-by-zeroabsolute-7hqb
\nvar repeatedNTimes = function(A) {\n const map = {};\n \n for (let i = 0; i < A.length; i++) {\n if (A[i] in map) {\n return A[i];\
zeroabsolute
NORMAL
2020-08-22T10:54:31.132648+00:00
2020-08-22T10:54:31.132693+00:00
372
false
```\nvar repeatedNTimes = function(A) {\n const map = {};\n \n for (let i = 0; i < A.length; i++) {\n if (A[i] in map) {\n return A[i];\n } else {\n map[A[i]] = 0;\n }\n }\n \n return 0;\n};\n```
4
0
['JavaScript']
0
n-repeated-element-in-size-2n-array
One line JavaScript beats 100%
one-line-javascript-beats-100-by-xeodou-uhol
\n/**\n * @param {number[]} A\n * @return {number}\n */\nvar repeatedNTimes = function(A) {\n return A.find((a, index, array) => array.indexOf(a) !== index)\n}
xeodou
NORMAL
2019-04-13T16:54:13.652105+00:00
2019-04-13T16:54:13.652146+00:00
294
false
```\n/**\n * @param {number[]} A\n * @return {number}\n */\nvar repeatedNTimes = function(A) {\n return A.find((a, index, array) => array.indexOf(a) !== index)\n};\n```
4
0
[]
0
n-repeated-element-in-size-2n-array
c# 100% o(n) time o(1) space solution with explanation
c-100-on-time-o1-space-solution-with-exp-pebc
\npublic int RepeatedNTimes(int[] A)\n{\n\t// Since half of the array elements are the repeated number.\n\t// if A.Length != 4, we can always find two adjacent
gary10
NORMAL
2019-01-31T13:17:13.514110+00:00
2019-01-31T13:17:13.514180+00:00
388
false
```\npublic int RepeatedNTimes(int[] A)\n{\n\t// Since half of the array elements are the repeated number.\n\t// if A.Length != 4, we can always find two adjacent repeated\n\t// elements seperated by at most 2\n\tfor(int i = 0; i < A.Length - 2; i++)\n\t{\n\t\tif(A[i] == A[i + 1] || A[i] == A[i + 2])\n\t\t{\n\t\t\tretu...
4
0
[]
2
n-repeated-element-in-size-2n-array
Java Trivial solution with O(1) Space and O(N) time.
java-trivial-solution-with-o1-space-and-ggi3u
class Solution {\n public int repeatedNTimes(int[] A) {\n int a = -1;\n int b = -1;\n int c = -1;\n\n for(int i = 0; i < A.length
pjfry
NORMAL
2019-01-29T06:46:56.459194+00:00
2019-01-29T06:46:56.459264+00:00
1,163
false
```class Solution {\n public int repeatedNTimes(int[] A) {\n int a = -1;\n int b = -1;\n int c = -1;\n\n for(int i = 0; i < A.length; i++){\n \tif(a == A[i]){\n \t\treturn a;\n \t}\n\n \tif(b == A[i]){\n \t\treturn b;\n \t}\n\n \tif(c == A[...
4
0
[]
4
n-repeated-element-in-size-2n-array
c# 1 liner
c-1-liner-by-dimager2003-skf3
\n\n public static int RepeatedNTimes(int[] A)\n {\n return (A.Sum() - A.Distinct().Sum()) / (A.Count() - A.Distinct().Count());\n }\n\
dimager2003
NORMAL
2018-12-23T06:26:04.998201+00:00
2018-12-23T06:26:04.998250+00:00
293
false
\n```\n public static int RepeatedNTimes(int[] A)\n {\n return (A.Sum() - A.Distinct().Sum()) / (A.Count() - A.Distinct().Count());\n }\n```\n\nthis 2 liner is faster though\n```\n public static int RepeatedNTimes(int[] A)\n {\n var distA = A.Distinct();\n return (A...
4
0
[]
1
n-repeated-element-in-size-2n-array
💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
easiestfaster-lesser-cpython3javacpython-q4o1
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n Describe your first thoughts on how to solve this problem. \n- J
Edwards310
NORMAL
2024-12-06T08:01:57.666546+00:00
2024-12-06T08:01:57.666569+00:00
400
false
![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/9fc46acb-7ba4-42e1-864c-3b0e7e0e82b6_1730795144.4340796.jpeg)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n- ***JavaScript Code -->**...
3
0
['Array', 'Hash Table', 'C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']
1
n-repeated-element-in-size-2n-array
Simple C++ solution using. count function
simple-c-solution-using-count-function-b-j3c7
Intuition\nIf count of the perticular element of the vector = vector size / 2 then return that element. \n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space
kirtanmatalia
NORMAL
2023-12-01T20:14:16.965656+00:00
2023-12-01T20:14:16.965684+00:00
423
false
# Intuition\nIf count of the perticular element of the vector = vector size / 2 then return that element. \n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& nums) \n {\n int n = nums.size()/2;\n\n for(...
3
0
['C++']
1
n-repeated-element-in-size-2n-array
Best Java Solution || Beats 100%
best-java-solution-beats-100-by-ravikuma-q4qz
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ravikumar50
NORMAL
2023-09-23T17:55:17.643784+00:00
2023-09-23T17:55:17.643803+00:00
421
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['Java']
0
n-repeated-element-in-size-2n-array
Python3: Two different approach, first run in O(nlogn) , the second O(n^2)
python3-two-different-approach-first-run-69av
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nTwo approaches: \nFirst
ResilientWarrior
NORMAL
2023-08-11T14:02:32.603834+00:00
2023-08-11T14:02:32.603851+00:00
22
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTwo approaches: \nFirst Approach\n - the method of solving the problem is sorting nums and then using linear search comparing each values, if you come across t...
3
0
['Python3']
1
n-repeated-element-in-size-2n-array
BEST SOLUTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
best-solution-by-akg30akg-nc1f
Intuition\nCount the number of times elements are repeated.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTake each element with
akg30akg
NORMAL
2022-12-08T18:20:52.878629+00:00
2022-12-08T18:20:52.878660+00:00
1,364
false
# Intuition\nCount the number of times elements are repeated.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTake each element with its count in the map data structure.\nCheck each element count,\nif its more than or equal to n/2, then return that element.\n<!-- Describe your approa...
3
0
['Array', 'Hash Table', 'Math', 'Counting', 'C++']
2
n-repeated-element-in-size-2n-array
C++ Most easy and understanding solution , beginner friendly.
c-most-easy-and-understanding-solution-b-klm7
If you find it helpfull then please consider it upvoting.\n\n\t\tsort(nums.begin(),nums.end());\n for(int i=1; i<nums.size(); i++)\n { if(nums[
Akshay_Saini
NORMAL
2022-10-29T09:31:06.106196+00:00
2022-10-29T09:32:32.166639+00:00
373
false
***If you find it helpfull then please consider it upvoting.***\n\n\t\tsort(nums.begin(),nums.end());\n for(int i=1; i<nums.size(); i++)\n { if(nums[i]==nums[i-1])\n return nums[i];\n }\n return -1; // This will disobey the constraints so it will be ignored.
3
0
['C']
0
n-repeated-element-in-size-2n-array
N-Repeated Element in Size 2N Array
n-repeated-element-in-size-2n-array-by-a-qatd
int repeatedNTimes(vector& nums) {\n mapm;\n int n=nums.size();\n for(int i=0; i<n; i++){\n m[nums[i]]++;\n }\n fo
Ashish_49
NORMAL
2022-08-14T16:45:14.522956+00:00
2022-08-14T16:45:14.522986+00:00
25
false
int repeatedNTimes(vector<int>& nums) {\n map<int, int>m;\n int n=nums.size();\n for(int i=0; i<n; i++){\n m[nums[i]]++;\n }\n for(auto x :m){\n if(x.second==n/2){\n return x.first;\n }\n }\n return -1;\n }
3
0
['C']
0
n-repeated-element-in-size-2n-array
99.55% runtime | O(1) space | Python
9955-runtime-o1-space-python-by-dylwu-3flw
\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n\t\t\n\t\t# keep track of the two previous numbers\n last2, last1 = nums[0], nu
dylwu
NORMAL
2022-02-21T15:23:57.105399+00:00
2022-02-21T15:23:57.105449+00:00
330
false
```\nclass Solution:\n def repeatedNTimes(self, nums: List[int]) -> int:\n\t\t\n\t\t# keep track of the two previous numbers\n last2, last1 = nums[0], nums[1]\n \n\t\t# edge case: first and last are the same\n if nums[0] == nums[-1]:\n return nums[0]\n \n for i in range(...
3
0
['Python']
0
n-repeated-element-in-size-2n-array
A simple solution based on majority voting
a-simple-solution-based-on-majority-voti-w2ak
\nint repeatedNTimes(vector<int>& nums) {\n\tint currCount=0, currNum=0;\n\tfor(auto num: nums) {\n\t\tif(currCount==0) { currNum=num; }\n\t\tcurrCount = num==c
jaisw7
NORMAL
2021-12-20T01:35:29.910952+00:00
2021-12-20T01:35:58.117781+00:00
176
false
```\nint repeatedNTimes(vector<int>& nums) {\n\tint currCount=0, currNum=0;\n\tfor(auto num: nums) {\n\t\tif(currCount==0) { currNum=num; }\n\t\tcurrCount = num==currNum ? currCount+1 : currCount-1;\n\t\tif(currCount>1) { return currNum; }\n\t}\n\n\t// either of the last two elements can be repeated N times\n\tif(count...
3
0
['C']
0
n-repeated-element-in-size-2n-array
[Python 3] Simple one-liner using zip() | Explained!
python-3-simple-one-liner-using-zip-expl-rr07
If you found this explanation and solution helpful, please upvote!!\n\n-------\n\nLet\'s take an example:\n\nnums = [1,2,3,3]\n\n1) Create a new sorted list (k
TP0604
NORMAL
2021-10-06T09:03:55.819084+00:00
2021-10-06T12:12:43.240163+00:00
574
false
If you found this explanation and solution helpful, please **upvote**!!\n\n-------\n\nLet\'s take an example:\n```\nnums = [1,2,3,3]\n```\n1) Create a new sorted list (`k = sorted(nums)`) or modify in-place (`nums.sort()`).\n2) Zip `k` and `k[1:]` to produce combinations as follows:\n\n ```\n k = [1, 2, 3, 3]...
3
0
['Python', 'Python3']
0
n-repeated-element-in-size-2n-array
⭐ Easy C++ Solution ⭐
easy-c-solution-by-divyaj101-eb0g
\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n unordered_set<int> seen;\n for (int a: A) {\n if (seen.count(a)
divyaj101
NORMAL
2021-06-30T13:28:05.668176+00:00
2021-06-30T13:31:43.685248+00:00
90
false
```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n unordered_set<int> seen;\n for (int a: A) {\n if (seen.count(a))\n return a;\n seen.insert(a);\n }\n return -1;\n }\n};\n```
3
0
['C']
0
n-repeated-element-in-size-2n-array
C++ - Using Iterative Way and Map
c-using-iterative-way-and-map-by-jay0411-523f
1. Simple for loop\n\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& nums) \n {\n int n = nums.size()/2;\n int arr[10000];\n
jay0411
NORMAL
2021-05-14T02:19:22.601555+00:00
2021-05-14T02:20:00.322374+00:00
185
false
**1. Simple for loop**\n```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& nums) \n {\n int n = nums.size()/2;\n int arr[10000];\n memset(arr, 0, sizeof(arr));\n for(int x : nums)\n arr[x]++;\n for(int i=0; i<10000; i++)\n {\n if(arr[i]...
3
0
['C', 'Iterator']
1
n-repeated-element-in-size-2n-array
Easy Solution with Set in Java
easy-solution-with-set-in-java-by-nitesh-5lp9
Why Set, Notice that here all the repeated elements are only repeated, rest all are unique.\nFor Input: [1,2,3,3], Only 3 is repeated 2 times, rest are unique.\
Nitesh_09
NORMAL
2021-04-10T17:16:30.728105+00:00
2021-04-10T17:16:30.728143+00:00
76
false
Why Set, Notice that here all the repeated elements are only repeated, rest all are unique.\nFor Input: [1,2,3,3], Only 3 is repeated 2 times, rest are unique.\nFor Input: [2,1,2,5,3,2] only 2 are repeated 3 times, rest are Unique.\nFor Input: [5,1,5,2,5,3,5,4], Only 5 are repeated 4 times.\n\nSo Just need to check if ...
3
0
[]
0
n-repeated-element-in-size-2n-array
C++ Super Simple, Short and Easy Solution
c-super-simple-short-and-easy-solution-b-boma
\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n set<int> s;\n for (auto a : A) {\n // If the number was seen al
yehudisk
NORMAL
2020-12-20T12:15:57.039823+00:00
2020-12-20T12:15:57.039859+00:00
225
false
```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n set<int> s;\n for (auto a : A) {\n // If the number was seen already, we know this is the result.\n if (s.find(a) != s.end())\n return a;\n s.insert(a);\n }\n return 0;\...
3
0
['C']
0
n-repeated-element-in-size-2n-array
C++ solution using map
c-solution-using-map-by-asif10h-uwar
\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n map<int, int> mp;\n map<int, int> :: iterator it;\n for(auto i : A)
asif10h
NORMAL
2020-06-20T18:29:40.136179+00:00
2020-06-20T18:29:40.136227+00:00
351
false
```\nclass Solution {\npublic:\n int repeatedNTimes(vector<int>& A) {\n map<int, int> mp;\n map<int, int> :: iterator it;\n for(auto i : A){\n mp[i]++;\n }\n int max = 0; \n int ans;\n for(auto it = mp.begin(); it != mp.end(); it++){\n if(it->se...
3
0
['C', 'C++']
3