📜  在给定的矩阵中找到具有最大唯一元素的行

📅  最后修改于: 2022-05-13 01:56:10.390000             🧑  作者: Mango

在给定的矩阵中找到具有最大唯一元素的行

给定一个大小为N*M的矩阵arr[][]任务是找到具有最大唯一元素的行的索引。如果可能有多行,则返回最小索引行。

例子:

方法:可以使用一组数据结构来解决该任务。遍历行并将相应的元素存储在集合中以跟踪唯一元素,返回具有最大唯一元素的最小索引行。

下面是上述方法的实现:

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 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


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 s = new HashSet();
 
    // 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 temp1 = new List();
    temp1 = (t1.ToList());
    List temp2 = new List();
    int []t2 = {1, 2, 2, 4, 7};
    temp2 = (t2.ToList());
    List temp3 = new List();
    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



输出
0

时间复杂度:O(N*M*logM)
辅助空间 O(M)