Write a program to print K-th repeating element from an array


Printing repeating element from an array
  1. import java.util.HashMap;
  2. import java.util.Map;


  3. public class RepeatingElement {
  4. public static void main(String[] args) {
  5. int arr[]={1,1,1,2,3,1,6,5,7,7,8,4,4,12,2,5,8,9,7,0,6,0,6,5,3,4,0,0,1};
  6. Map m=new HashMap();
  7. for(int i=0;i<arr.length;i++){
  8. int total=0;
  9. for(int j=i+1;j<arr.length;j++){
  10. if(!m.containsKey(arr[i]+"")){
  11. if(arr[i]==arr[j]){
  12. total=total+1;
  13. }
  14. }
  15. }
  16. if(total>0){
  17. m.put(arr[i]+"", total+"");
  18. }
  19. }
  20. System.out.println(m);
  21. }
  22. }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations