Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, May 6, 2017

Love For The Twins using hashmap


Problem : Find numbers which are same for  2 times , i.e even in count

Explanation:

create a hasmap which will give number and its count and in last we are checking even counts and printing the 2*count.

Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class repeat
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int max=0;
   int n=ab.nextInt();
   HashMap<Integer,Integer> hmap =new HashMap<Integer,Integer>();
while(n-->0)
{
int temp1=ab.nextInt();
int count=1;
if(hmap.containsKey(temp1))
count+=hmap.get(temp1);
hmap.put(temp1,count);
}
for(int c:hmap.values())
{
max+=(c/2);
}
System.out.println(2*max);
}
}
}

0 Comments:

Post a Comment

Stats