Skip to content

Instantly share code, notes, and snippets.

@ShubhamS32
Created March 23, 2019 19:25
Show Gist options
  • Select an option

  • Save ShubhamS32/abc5bc88cc5d2576063ece2350ade306 to your computer and use it in GitHub Desktop.

Select an option

Save ShubhamS32/abc5bc88cc5d2576063ece2350ade306 to your computer and use it in GitHub Desktop.
Find pair of Socks pair
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.lang.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class Solution {
// Complete the sockMerchant function below.
static int sockMerchant(int n, int[] ar) {
Map<Integer,Integer> countt = new HashMap<Integer,Integer>();
boolean isEven =false;
int counter=0;
List<Integer> myTemp = new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
myTemp.add(ar[i]);
//System.out.println("Items"+ar[i]);
}
long l = myTemp.stream().distinct().count();
List<Integer> listDistinctInts = myTemp.stream().distinct().collect(Collectors.toList());
System.out.println("Unique :"+l);
for(int i=0;i<listDistinctInts.size();i++)
{
int count =getCount(listDistinctInts.get(i),ar);
System.out.println("No :"+listDistinctInts.get(i)+"| Count :"+count);
int noOfOccurance=count/2;
counter=counter+noOfOccurance;
count=0;
}
return counter;
}
private static int getCount(int i, int[] ar)
{
int counter=0;
System.out.println("Input Number :"+i+":"+ar.length);
for(int j=0;j<ar.length;j++)
{
System.out.println("ar is"+ar[j]);
if(i==ar[j])
{
System.out.println("Match for"+i);
counter++;
}
}
System.out.println("Counter Value="+counter);
return counter;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] ar = new int[n];
String[] arItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int arItem = Integer.parseInt(arItems[i]);
ar[i] = arItem;
}
int result = sockMerchant(n, ar);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment