q2

import java.util.HashMap;

import java.util.Map;

public class q2

{

public static int findRepetitiveElement(int[] A)

{

Map<Integer, Boolean> hashTable = new HashMap<>();

for (int num : A)

{

if (hashTable.containsKey(num))

{

return num;

}

else

{

hashTable.put(num, true);

}

}

return -1;

}

public static void main(String[] args)

{

int[] A = {1, 2, 3, 2, 2, 5, 6};

int repetitiveElement = findRepetitiveElement(A);

System.out.println(“Repetitive element: “ + repetitiveElement);

}

}

Paste text,images,html and share with anyone
Scroll to Top