Replace the Array Elements by Its Corresponding Rank in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have given an array with some random integer values and we have to replace those elements with their corresponding ranks. So, we have to first sort the given array in ascending order to find the ranks. After finding the ranks we just replace those ranks values with their respective array’s elements.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Given Array= [12,23,34,45,15].
By sorting the given array in ascending order= [12,15,23,34,45]
So, if we replace the elements with their corresponding array’s elements, we get = [1, 3, 4, 5, 2]

Instance-2

Given Array= [38,94,86,63,36].
By sorting the given array in ascending order= [36,38,63,86,94]
So, if we replace the elements with their corresponding array’s elements, we get = [2, 5, 4, 3, 1]

Instance-3

Given Array= [54,67,23,95,24].
By sorting the given array in ascending order= [23,24,54,67,95]
So, if we replace the elements with their corresponding array’s elements, we get = [3, 4, 1, 5, 2]

Algorithm:

  • Step 1 − Declare an array and initialize it with some random integer values.

  • Step 2 − Declare another array which is a temporary array and store the copied elements of the input array.

  • Step 3 − Sort that temporary array in ascending order, so that we can get the ranks.

  • Step 4 − Take a nested for loop to compare between the ranks with their corresponding array elements in a given array and store the ranks in place of their previous values.

  • Step 5 − After getting the whole array with replaced ranks, print that array as output.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array. length

where, ‘array’ refers to the array reference.

You can use Arrays.sort() method to sort the array in ascending order.

Arrays.sort(array_name);

You can use of Arrays.copyOfRange() method to copy the elements of an array.

Arrays.copyOfRange(array_name, 0, array_name.length);

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Nested for Loop

  • By User Hash Map

Let’s see the program along with its output one by one.

Approach-1: By Using Nested for Loop Method

In this approach, we declare an array with some random integer values and by using a nested for loop method; we replace the array elements by its corresponding rank and print that array as output.

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
      
      // declare an integer type of array and store some random value to it by static input method
      int[] inputArray = { 122, 34, 25, 67 , 87};
      
      // call the user-defined method and pass the inputArray[]
      rankedArray(inputArray);
      
      // Print the array as output
      System.out.println("Array with replaced ranks = " + Arrays.toString(inputArray));
   }
   //user-defined method to replace the elements of given array by their ranks
   static void rankedArray(int[] inpArr) {
      // declare a temporary array and store the copied array of input array
      int temp[] = Arrays.copyOfRange(inpArr, 0, inpArr.length);
      // Sort the values of temp[] array in ascending order Arrays.sort(temp);
      //initiate the nested-loop to find the corresponding position of given array
      for(int i=0; i< inpArr.length; i++){
         for(int j=0; j< inpArr.length; j++){
            if(temp[j]==inpArr[i]){
               inpArr[i] = j+1;
               break;
            }
         }
      }
   }
}

Output

Array with replaced ranks = [5, 2, 1, 3, 4]

Approach-2: By Using Hashmap

In this approach, we declare an array with some random integer values and by using the hash map method; we replace the array elements by their corresponding rank and print that array as output.

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
      
      // declare and initialize integer type array
      int[] inputArray = { 34,53,12,64,76};
      
      // call the user-defined method
      rankedArray(inputArray);
      
      // Print the array as output
      System.out.println("Array with replaced ranks = " +
      Arrays.toString(inputArray));
   }
   
   //user-defined method to replace the elements of given array by their ranks
   static void rankedArray(int[] inpArr) {
   
      // declare a temporary array and store the copied inputed array
      int temp[] = Arrays.copyOfRange(inpArr, 0, inpArr.length);
      
      // Sort the temp[] array in ascending order
      Arrays.sort(temp);
      
      // create object of HashMap class to store the maped rank of array's elements
      Map hashRanks = new HashMap<>();
      
      //declare an integer to store the value of corresponding Ranks of array's elements
      int correspondingRank = 1;
      
      //intiate the loop
      for (int i = 0; i < temp.length; i++) {
         
         //declare an integer to store the elements of array
         int num = temp[i];
         
         // Update the corresponding Ranks of array's elements
         if (hashRanks.get(num) == null) {
            hashRanks.put(num, correspondingRank);
            correspondingRank++;
         }
      }
      
      // Assign the corresponding Ranks to the array's elements
      for (int i = 0; i < inpArr.length; i++) {
         int num = inpArr[i];
         inpArr[i] = (int)hashRanks.get(inpArr[i]);
      }
   }
}

Output

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Array with replaced ranks = [2, 3, 1, 4, 5]

In this article, we explored how to replace the array elements with -6 if the last digit of any element is 6 by using Java programming language.

Updated on: 02-Feb-2023

831 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements