Get the best out of our app
GFG App
Open App
Browser
Continue

# Find the row with maximum unique elements in given Matrix

Given a matrix arr[][] of size N*M The task is to find the index of the row that has the maximum unique elements. If there are multiple rows possible, return the minimum indexed row.

Examples:

Input: arr[][] = { {1, 2, 3, 4, 5}, {1, 2, 2, 4, 7}, {1, 3, 1, 3, 1} }
Output: 0
Explanation: Rows 0, 1 & 2 have 5, 4 & 2 unique elements respectively.

Input: arr[][] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 2, 3} }
Output : 0

Approach: The task can be solved using a set data structure. Iterate over the rows and store the corresponding element inside a set to keep track of the unique elements, return the minimum indexed row with maximum unique elements.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find the minimum indexed row` `// with maximum unique elements` `int` `get(``int` `n, ``int` `m, vector >& v)` `{` `    ``// Stores the unique elements` `    ``set<``int``> s;`   `    ``// Stores the minimum indexed row` `    ``int` `max_ans = INT_MAX;` `    ``int` `cnt = -1;`   `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``for` `(``int` `j = 0; j < m; j++) {` `            ``s.insert(v[i][j]);` `        ``}` `        ``int` `size = (``int``)s.size();` `        ``if` `(cnt < size) {` `            ``size = cnt;` `            ``max_ans = min(max_ans, i);` `        ``}` `        ``s.clear();` `    ``}` `    ``return` `max_ans;` `}`   `// Driver Code` `int` `main()` `{`   `    ``vector > arr` `        ``= { { 1, 2, 3, 4, 5 },` `            ``{ 1, 2, 2, 4, 7 },` `            ``{ 1, 3, 1, 3, 1 } };` `    ``int` `n = arr.size();` `    ``int` `m = arr[0].size();` `    ``cout << get(n, m, arr);` `}`

## Java

 `// JAVA program for the above approach` `import` `java.util.*;` `class` `GFG` `{`   `  ``// Function to find the minimum indexed row` `  ``// with maximum unique elements` `  ``public` `static` `int` `get(``int` `n, ``int` `m,` `                        ``ArrayList > v)` `  ``{` `    ``// Stores the unique elements` `    ``HashSet s = ``new` `HashSet();`   `    ``// Stores the minimum indexed row` `    ``int` `max_ans = Integer.MAX_VALUE;` `    ``int` `cnt = -``1``;`   `    ``for` `(``int` `i = ``0``; i < n; i++) {` `      ``for` `(``int` `j = ``0``; j < m; j++) {` `        ``s.add(v.get(i).get(j));` `      ``}` `      ``int` `size = (``int``)s.size();` `      ``if` `(cnt < size) {` `        ``size = cnt;` `        ``max_ans = Math.min(max_ans, i);` `      ``}` `      ``s.clear();` `    ``}` `    ``return` `max_ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args)` `  ``{`   `    ``ArrayList > arr` `      ``= ``new` `ArrayList >();` `    ``ArrayList temp1` `      ``= ``new` `ArrayList<>(Arrays.asList(``1``, ``2``, ``3``, ``4``, ``5``));` `    ``ArrayList temp2` `      ``= ``new` `ArrayList<>(Arrays.asList(``1``, ``2``, ``2``, ``4``, ``7``));` `    ``ArrayList temp3` `      ``= ``new` `ArrayList<>(Arrays.asList(``1``, ``3``, ``1``, ``3``, ``1``));` `    ``arr.add(temp1);` `    ``arr.add(temp2);` `    ``arr.add(temp3);` `    ``int` `n = arr.size();` `    ``int` `m = arr.get(``0``).size();` `    ``System.out.print(get(n, m, arr));` `  ``}` `}`   `// This code is contributed by Taranpreet`

## Python3

 `# Python program for the above approach`   `# Function to find the minimum indexed row` `# with maximum unique elements` `def` `get(n, m, v):` `    ``s ``=` `set``()` `    `  `    ``# Stores the minimum indexed row` `    ``max_ans ``=` `1000000000` `    ``cnt ``=` `-``1` `    ``for` `i ``in` `range``(``0``, n):` `        ``for` `j ``in` `range``(``0``, m):` `            ``s.add(v[i][j])` `            ``size ``=` `len``(s)` `            ``if` `cnt < size:` `                ``size ``=` `cnt` `                ``max_ans ``=` `min``(max_ans, i)` `            ``s.clear()` `    ``return` `max_ans`   `if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[[``1``, ``2``, ``3``, ``4``, ``5``],` `           ``[``1``, ``2``, ``2``, ``4``, ``7``],` `           ``[``1``, ``3``, ``1``, ``3``, ``1``]]` `    ``n ``=` `3` `    ``m ``=` `5` `    ``print``(get(n, m, arr))`   `# This code is contributed by Rohit Pradhan`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Linq;` `using` `System.Collections.Generic;`   `public` `class` `GFG {`   `  ``// Function to find the minimum indexed row` `  ``// with maximum unique elements` `  ``public` `static` `int` `get``(``int` `n, ``int` `m, List> v) {` `    ``// Stores the unique elements` `    ``HashSet<``int``> s = ``new` `HashSet<``int``>();`   `    ``// Stores the minimum indexed row` `    ``int` `max_ans = ``int``.MaxValue;` `    ``int` `cnt = -1;`   `    ``for` `(``int` `i = 0; i < n; i++) {` `      ``for` `(``int` `j = 0; j < m; j++) {` `        ``s.Add(v[i][j]);` `      ``}` `      ``int` `size = (``int``) s.Count;` `      ``if` `(cnt < size) {` `        ``size = cnt;` `        ``max_ans = Math.Min(max_ans, i);` `      ``}` `      ``s.Clear();` `    ``}` `    ``return` `max_ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(String[] args) {`   `    ``List> arr = ``new` `List>();` `    ``int` `[] t1 = {1, 2, 3, 4, 5};` `    ``List<``int``> temp1 = ``new` `List<``int``>();` `    ``temp1 = (t1.ToList());` `    ``List<``int``> temp2 = ``new` `List<``int``>();` `    ``int` `[]t2 = {1, 2, 2, 4, 7};` `    ``temp2 = (t2.ToList());` `    ``List<``int``> temp3 = ``new` `List<``int``>();` `    ``int``[] t3 = {1, 3, 1, 3, 1};` `    ``temp3 = t3.ToList();` `    ``arr.Add(temp1);` `    ``arr.Add(temp2);` `    ``arr.Add(temp3);` `    ``int` `n = arr.Count;` `    ``int` `m = arr[0].Count;` `    ``Console.Write(``get``(n, m, arr));` `  ``}` `}`   `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output

`0`

Time Complexity: O(N*M*logM)
Auxiliary Space: O(M)

My Personal Notes arrow_drop_up
Similar Reads
Related Tutorials