Answers for "Given an array of integers with repeating elements, find sum of differences between positions of repeated elements and store them in an array of same size."

0

first duplicate in array

import java.util.*; 
  
class Main 
{ 
    // This function prints the first repeating element in arr[] 
    static void printFirstRepeating(int arr[]) 
    { 
        // Initialize index of first repeating element 
        int min = -1; 
  
        // Creates an empty hashset 
        HashSet<Integer> set = new HashSet<>(); 
  
        // Traverse the input array from right to left 
        for (int i=arr.length-1; i>=0; i--) 
        { 
            // If element is already in hash set, update min 
            if (set.contains(arr[i])) 
                min = i; 
  
            else   // Else add element to hash set 
                set.add(arr[i]); 
        } 
  
        // Print the result 
        if (min != -1) 
          System.out.println("The first repeating element is " + arr[min]); 
        else
          System.out.println("There are no repeating elements"); 
    } 
  
    // Driver method to test above method 
    public static void main (String[] args) throws java.lang.Exception 
    { 
        int arr[] = {10, 5, 3, 4, 3, 5, 6}; 
        printFirstRepeating(arr); 
    } 
}
Posted by: Guest on March-17-2020
1

java find duplicates in array

// Uses a set, which does not allow duplicates 

for (String name : names) 
{
     if (set.add(name) == false) 
     {
        // print name your duplicate element
     }
}
Posted by: Guest on May-09-2020
0

Given an array of integers with repeating elements, find sum of differences between positions of repeated elements and store them in an array of same size.

10
20
10
Posted by: Guest on March-16-2021

Code answers related to "Given an array of integers with repeating elements, find sum of differences between positions of repeated elements and store them in an array of same size."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language