Posted on by Kalkicode
Code Hash

# Maximum distance between two occurrences of same element

Here given code implementation process.

``````/*
Java Program
Maximum distance between two occurrences of same element
*/
import java.util.HashMap;
public class Distance
{
// Display given array element
public void display(int[] arr, int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
// Find max distance between any two similar elements
public void maxDistance(int[] data, int n)
{
// Resultant variable
int result = 0;
int element = 0;
// Use to collect element occurrence
HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
// Display given array element
System.out.print(" Given Array  : ");
display(data, n);
// Execute loop through by array size
for (int i = 0; i < n; ++i)
{
if (record.containsKey(data[i]))
{
if (result < (i - record.get(data[i])))
{
// Get new result
result = i - record.get(data[i]);
element = data[i];
}
}
else
{
record.put(data[i], i);
}
}
// Display calculated result
System.out.print("\n Element is "+element+" and Distance is : " + result);
}
public static void main(String[] arg)
{
// Define array of integer elements
int[] data = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
// Get the number of elements
int n = data.length;
// test
}
}``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;

/*
C++ Program
Maximum distance between two occurrences of same element
*/
class Distance
{
public:
// Display given array element
void display(int arr[], int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Find max distance between any two similar elements
void maxDistance(int data[], int n)
{
// Resultant variable
int result = 0;
int element = 0;
// Use to collect element occurrence
unordered_map < int, int > record ;
// Display given array element
cout << " Given Array  : ";
this->display(data, n);
// Execute loop through by array size
for (int i = 0; i < n; ++i)
{
if (record.find(data[i]) != record.end())
{
if (result < (i - record[data[i]]))
{
// Get new result
result = i - record[data[i]];
element = data[i];
}
}
else
{
record[data[i]] = i;
}
}
// Display calculated result
cout << "\n Element is " << element << " and Distance is : " << result;
}
};
int main()
{
// Define array of integer elements
int data[] = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
// Get the number of elements
int n = sizeof(data) / sizeof(data[0]);
// test
return 0;
}``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Maximum distance between two occurrences of same element
*/
public class Distance
{
// Display given array element
public void display(int[] arr, int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Find max distance between any two similar elements
public void maxDistance(int[] data, int n)
{
// Resultant variable
int result = 0;
int element = 0;
// Use to collect element occurrence
Dictionary < int, int > record = new Dictionary < int, int > ();
// Display given array element
Console.Write(" Given Array  : ");
display(data, n);
// Execute loop through by array size
for (int i = 0; i < n; ++i)
{
if (record.ContainsKey(data[i]))
{
if (result < (i - record[data[i]]))
{
// Get new result
result = i - record[data[i]];
element = data[i];
}
}
else
{
}
}
// Display calculated result
Console.Write("\n Element is " + element + " and Distance is : " + result);
}
public static void Main(String[] arg)
{
// Define array of integer elements
int[] data = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
// Get the number of elements
int n = data.Length;
// test
}
}``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````<?php
/*
Php Program
Maximum distance between two occurrences of same element
*/
class Distance
{
// Display given array element
public	function display( & \$arr, \$n)
{
// iterate the loop through by n
for (\$i = 0; \$i < \$n; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Find max distance between any two similar elements
public	function maxDistance( & \$data, \$n)
{
// Resultant variable
\$result = 0;
\$element = 0;
// Use to collect element occurrence
\$record = array();
// Display given array element
echo " Given Array  : ";
\$this->display(\$data, \$n);
// Execute loop through by array size
for (\$i = 0; \$i < \$n; ++\$i)
{
if (array_key_exists(\$data[\$i], \$record))
{
if (\$result < (\$i - \$record[\$data[\$i]]))
{
// Get new result
\$result = \$i - \$record[\$data[\$i]];
\$element = \$data[\$i];
}
}
else
{
\$record[\$data[\$i]] = \$i;
}
}
// Display calculated result
echo "\n Element is ". \$element ." and Distance is : ". \$result;
}
}

function main()
{
// Define array of integer elements
\$data = array(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
// Get the number of elements
\$n = count(\$data);
}
main();``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````/*
Node Js Program
Maximum distance between two occurrences of same element
*/
class Distance
{
// Display given array element
display(arr, n)
{
// iterate the loop through by n
for (var i = 0; i < n; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Find max distance between any two similar elements
maxDistance(data, n)
{
// Resultant variable
var result = 0;
var element = 0;
// Use to collect element occurrence
var record = new Map();
// Display given array element
process.stdout.write(" Given Array  : ");
this.display(data, n);
// Execute loop through by array size
for (var i = 0; i < n; ++i)
{
if (record.has(data[i]))
{
if (result < (i - record.get(data[i])))
{
// Get new result
result = i - record.get(data[i]);
element = data[i];
}
}
else
{
record.set(data[i], i);
}
}
// Display calculated result
process.stdout.write("\n Element is " + element + " and Distance is : " + result);
}
}

function main()
{
// Define array of integer elements
var data = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6];
// Get the number of elements
var n = data.length;
// test
}
main();``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````#    Python 3 Program
#    Maximum distance between two occurrences of same element

class Distance :
#  Display given array element
def display(self, arr, n) :
#  iterate the loop through by n
i = 0
while (i < n) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Find max distance between any two similar elements
def maxDistance(self, data, n) :
#  Resultant variable
result = 0
element = 0
#  Use to collect element occurrence
record = dict()
#  Display given list element
print(" Given Array  : ", end = "")
self.display(data, n)
i = 0
#  Execute loop through by list size
while (i < n) :
if (data[i] in record.keys()) :
if (result < (i - record.get(data[i]))) :
#  Get new result
result = i - record.get(data[i])
element = data[i]

else :
record[data[i]] = i

i += 1

#  Display calculated result
print("\n Element is ", element ," and Distance is : ", result, end = "")

def main() :
#  Define list of integer elements
data = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6]
#  Get the number of elements
n = len(data)
#  test

if __name__ == "__main__": main()``````

#### Output

`````` Given Array  :    3   5   7   9   4   3   5   7   4   7   9   5   3   9   5   6

Element is  5  and Distance is :  13``````
``````#  Ruby Program
#  Maximum distance between two occurrences of same element

class Distance
#  Display given array element
def display(arr, n)
#  iterate the loop through by n
i = 0
while (i < n)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Find max distance between any two similar elements
def maxDistance(data, n)
#  Resultant variable
result = 0
element = 0
#  Use to collect element occurrence
record = Hash.new
#  Display given array element
print(" Given Array  : ")
self.display(data, n)
i = 0
#  Execute loop through by array size
while (i < n)
if (record.key?(data[i]))
if (result < (i - record[data[i]]))
#  Get new result
result = i - record[data[i]]
element = data[i]
end
else
record[data[i]] = i
end

i += 1
end

#  Display calculated result
print("\n Element is ", element ," and Distance is : ", result)
end

end

def main()
#  Define array of integer elements
data = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6]
#  Get the number of elements
n = data.length
#  test
end

main()``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````import scala.collection.mutable._;
/*
Scala Program
Maximum distance between two occurrences of same element
*/
class Distance
{
// Display given array element
def display(arr: Array[Int], n: Int): Unit = {
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Find max distance between any two similar elements
def maxDistance(data: Array[Int], n: Int): Unit = {
// Resultant variable
var result: Int = 0;
var element: Int = 0;
// Use to collect element occurrence

var record: Map[Int, Int] = Map();
// Display given array element
print(" Given Array  : ");
this.display(data, n);
var i: Int = 0;
// Execute loop through by array size
while (i < n)
{
if (record.contains(data(i)))
{
if (result < (i - record.get(data(i)).get))
{
// Get new result
result = i - record.get(data(i)).get;
element = data(i);
}
}
else
{
}
i += 1;
}
// Display calculated result
print("\n Element is " + element + " and Distance is : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Distance = new Distance();
// Define array of integer elements
var data: Array[Int] = Array(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
// Get the number of elements
var n: Int = data.length;
// test
}
}``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````
``````import Foundation
/*
Swift 4 Program
Maximum distance between two occurrences of same element
*/
class Distance
{
// Display given array element
func display(_ arr: [Int], _ n: Int)
{
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find max distance between any two similar elements
func maxDistance(_ data: [Int], _ n: Int)
{
// Resultant variable
var result: Int = 0;
var element: Int = 0;
// Use to collect element occurrence
var record = [Int: Int]();
// Display given array element
print(" Given Array  : ", terminator: "");
self.display(data, n);
var i: Int = 0;
// Execute loop through by array size
while (i < n)
{
if (record.keys.contains(data[i]))
{
if (result < (i - record[data[i]]!))
{
// Get new result
result = i - record[data[i]]!;
element = data[i];
}
}
else
{
record[data[i]] = i;
}
i += 1;
}
// Display calculated result
print("\n Element is ", element ," and Distance is : ", result, terminator: "");
}
}
func main()
{
// Define array of integer elements
let data: [Int] = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6];
// Get the number of elements
let n: Int = data.count;
// test
}
main();``````

#### Output

`````` Given Array  :    3   5   7   9   4   3   5   7   4   7   9   5   3   9   5   6

Element is  5  and Distance is :  13``````
``````/*
Kotlin Program
Maximum distance between two occurrences of same element
*/
class Distance
{
// Display given array element
fun display(arr: Array < Int > , n: Int): Unit
{
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Find max distance between any two similar elements
fun maxDistance(data: Array < Int > , n: Int): Unit
{
// Resultant variable
var result: Int = 0;
var element: Int = 0;
// Use to collect element occurrence
var record = mutableMapOf<Int, Int>();
// Display given array element
print(" Given Array  : ");
this.display(data, n);
var i: Int = 0;
// Execute loop through by array size
while (i < n)
{
if (record.containsKey(data[i]))
{
if (result < (i - record.getValue(data[i])))
{
// Get new result
result = i - record.getValue(data[i]);
element = data[i];
}
}
else
{
record.put(data[i], i);
}
i += 1;
}
// Display calculated result
print("\n Element is " + element + " and Distance is : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
var data: Array < Int > = arrayOf(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
// Get the number of elements
var n: Int = data.count();
// test
}``````

#### Output

`````` Given Array  :   3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6

Element is 5 and Distance is : 13``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post