Open In App

Find the row with maximum unique elements in given Matrix

Last Updated : 17 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum indexed row
// with maximum unique elements
int get(int n, int m, vector<vector<int> >& 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<vector<int> > 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<ArrayList<Integer> > v)
  {
    // Stores the unique elements
    HashSet<Integer> s = new HashSet<Integer>();
 
    // 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<ArrayList<Integer> > arr
      = new ArrayList<ArrayList<Integer> >();
    ArrayList<Integer> temp1
      = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
    ArrayList<Integer> temp2
      = new ArrayList<>(Arrays.asList(1, 2, 2, 4, 7));
    ArrayList<Integer> 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<List<int>> 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<List<int>> arr = new List<List<int>>();
    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




<script>
       // JavaScript code for the above approach
 
       // Function to find the minimum indexed row
       // with maximum unique elements
       function get(n, m, v) {
           // Stores the unique elements
           let s = new Set();
 
           // Stores the minimum indexed row
           let max_ans = Number.MAX_VALUE;
           let cnt = -1;
 
           for (let i = 0; i < n; i++) {
               for (let j = 0; j < m; j++) {
                   s.add(v[i][j]);
               }
               let size = s.size;
               if (cnt < size) {
                   size = cnt;
                   max_ans = Math.min(max_ans, i);
               }
               s.clear();
           }
           return max_ans;
       }
 
       // Driver Code
 
 
       let arr
           = [[1, 2, 3, 4, 5],
           [1, 2, 2, 4, 7],
           [1, 3, 1, 3, 1]];
       let n = arr.length;
       let m = arr[0].length;
       document.write(get(n, m, arr));
 
      // This code is contributed by Potta Lokesh
   </script>


 
 

Output

0

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads