📌  相关文章
📜  用大于最大值的值替换重复元素

📅  最后修改于: 2021-10-27 07:36:54             🧑  作者: Mango

给定一个整数数组,如果一个整数是重复的,则用一个大于该数字的数字替换它,该数字尚未插入数组中。
例子:

Input : arr = {1, 3, 4, 5, 3}
Output : 1 3 4 5 6
Here 3 is repeating so it is replaced with 6

Input : arr = {1, 3, 4, 4, 5, 3}
Output : 1 3 4 6 5 7

我们需要用一个没有出现在数组中并且比数组中存在的最大值大 1 的数字替换重复的数字。
来源:Paytm 面试经验(后端开发人员)。
计算数组中的最大元素并将重复元素替换为 maxx+1 并更新已访问的 maxx 元素。在实现中,使用了散列的基本概念。

C++
// CPP program to replace repeating elements
// with greater than the greatest.
#include 
using namespace std;
 
void replaceElements(int arr[], int n)
{
    // Maximum element in an array
    int maxx = *max_element(arr, arr+n);
 
    unordered_set s;
 
    for (int i = 0; i < n; i++) {
          
        // check whether the element is
        // repeated or not
        if (s.find(arr[i]) == s.end())
            s.insert(arr[i]);
 
        else {
 
            // update the repeated element with the
            // maxx element
            arr[i] = maxx + 1;
 
            maxx++; // update the max
 
            // mark the maximum element as visited
            s.insert(maxx);
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 4, 5, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    replaceElements(arr, n);
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}


Java
// Java  program to replace repeating elements
// with greater than the greatest.
import java.util.*;
class Solution
{
    //returns the maximum element
    static int max_element(int arr[])
    {
        int max=arr[0];
        for(int i=1;i s=new Vector();
  
    for (int i = 0; i < n; i++) {
           
        // check whether the element is
        // repeated or not
        if (!s.contains(arr[i]))
            s.add(arr[i]);
  
        else {
  
            // update the repeated element with the
            // maxx element
            arr[i] = maxx + 1;
  
            maxx++; // update the max
  
            // mark the maximum element as visited
            s.add(maxx);
        }
    }
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 3, 4, 5, 3 };
    int n = arr.length;
    replaceElements(arr, n);
    for (int i = 0; i < n; i++)
        System.out.print( arr[i] + " ");
     
}
}
//contributed by Arnab Kundu


Python3
# Python 3 program to replace repeating
# elements with greater than the greatest.
 
def replaceElements( arr, n):
 
    # Maximum element in an array
    maxx = max(arr)
 
    s = []
 
    for i in range (n) :
         
        # check whether the element is
        # repeated or not
        if arr[i] not in s:
            s.append(arr[i])
 
        else:
 
            # update the repeated element
            # with the maxx element
            arr[i] = maxx + 1
 
            maxx += 1 # update the max
 
            # mark the maximum element
            # as visited
            s.append(maxx)
 
# Driver code
if __name__ =="__main__":
 
    arr = [ 1, 3, 4, 5, 3 ]
    n = len(arr)
    replaceElements(arr, n)
    for i in range( n):
        print (arr[i], end = " ")
 
# This code is contributed by ita_c


C#
// C# program to replace repeating elements
// with greater than the greatest.
using System;
using System.Collections.Generic;
 
class GFG
{
    //returns the maximum element
    static int max_element(int []arr)
    {
        int max = arr[0];
        for(int i = 1; i < arr.Length; i++)
        {
            if(max < arr[i])
            max = arr[i];
        }
        return max;
    }
     
static void replaceElements(int []arr, int n)
{
    // Maximum element in an array
    int maxx = max_element(arr);
 
    List s = new List();
 
    for (int i = 0; i < n; i++)
    {
             
        // check whether the element is
        // repeated or not
        if (!s.Contains(arr[i]))
            s.Add(arr[i]);
 
        else
        {
 
            // update the repeated element with the
            // maxx element
            arr[i] = maxx + 1;
 
            maxx++; // update the max
 
            // mark the maximum element as visited
            s.Add(maxx);
        }
    }
}
 
// Driver code
public static void Main()
{
    int []arr = { 1, 3, 4, 5, 3 };
    int n = arr.Length;
    replaceElements(arr, n);
    for (int i = 0; i < n; i++)
        Console.Write( arr[i] + " ");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
1 3 4 5 6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程