Repeating element GFG

 package com.company;


public class RepeatingElement {
// EFFICIENT SOLUTION
public static int getRepeatedElement(int[] arr) {
int n = arr.length;
boolean[] visited = new boolean[n];
for(int i = 0; i< n; i++) {
visited[i] = false;
}
for(int i = 0; i< n; i++) {
if(visited[arr[i]]){
return arr[i];
}
visited[arr[i]] = true;
}
return -1;
}
// More efficient solution
public static int get(int[] arr){
int n = arr.length;
int slow = arr[0] + 1;
int fast = arr[0] + 1;
do{
slow = arr[slow] + 1;
fast = arr[arr[fast] + 1] + 1;
}while(slow != fast);

slow = arr[0] + 1;
while(slow != fast) {
slow = arr[slow] + 1;
fast = arr[fast] + 1;
}
return slow - 1;
}
public static void main(String[] args) {
int[] array = {0,3,1,3,4,2};
System.out.println(get(array));
}
}

Comments