1

There is an array consisting several numbers. Among them find out the pair number(that consist 2 times in that array) in java. suppose {2,5,7,8,2,3,5,6,5} in this array 2 consist 2 times so its is pair number.

i tried this way :

HashMap<Integer, Integer> hmap = new HashMap<>();
  for (int i = 0; i < arr.length; i++)
    {
        Integer c = hmap.get(arr[i]);
        if (hmap.get(arr[i]) == null)
               hmap.put(arr[i], 1);
        else
          hmap.put(arr[i], ++c);
    }
Kaustav
  • 69
  • 3
  • 12

5 Answers5

2

you can use streams

List<Integer> collect = Stream.of(2, 5, 7, 8, 2, 3, 5, 6, 5)
            .collect(Collectors.groupingBy(e -> e)).entrySet().stream()
            .filter(e -> e.getValue().size() == 2).map(Map.Entry::getKey)
PawelN
  • 848
  • 7
  • 17
1

Program using primitive type

num[j]= '0'; // this is just a placeholder

  int []num = {2,5,7,8,2,3,5,6};
        int counter = 0;
        for(int i=0;i<num.length-1;i++){
            for(int j=i+1;j<num.length && num[i]!=0;j++){
                if(num[i] == num[j]){
                    counter++;
                    num[j]= 0;
                }
            }
            if(counter==1)
                 System.out.println("pair found for: "+num[i]);
            counter = 0;
        }
divine
  • 4,746
  • 3
  • 27
  • 38
  • He already have a solution to find the pairs. And you should break the loop in some case (if num[i] = 0, then this is a checked value, skip it) – AxelH Nov 29 '16 at 07:44
  • o/p is : pair found for: 2 pair found for: 5 // 5 occurs 3 times in the array then how it can be pair? – Kaustav Nov 29 '16 at 07:51
  • @AxelH i opened the question at the beginning and i didnot find the hashmap code at that time. i forgot to refresh the browser. sorry. and i fixed the answer – divine Nov 29 '16 at 08:01
0

I can think of 2 ways. First you could use a map, in this method you would take each element in an array and put it inside a map so that each time you iterate you could check if you already insert that value into the map.

The second method would be similar to a sort, but instead you could check each element in the array with all the following elements and as soon as you find a match you would return and exit. This method would be slower than the map method I think but faster than sorting it first and then finding adjacent values.

George Miranda
  • 150
  • 1
  • 13
  • Also this is assuming there is only 1 pair, since you said "find out the pair" – George Miranda Nov 29 '16 at 06:59
  • `check each element in the array with all the following elements and as soon as you find a match you would return and exit` How would you know that the value in position 2 is not the equivalent of the value in position 0, already checked ? EDIT : Yep, your comment kind of answer my question ^^ – AxelH Nov 29 '16 at 07:02
0

Not posting the actual code, put that in Map and look for count. But you need to be careful to check for counting of 2s. Its not just 2, all counts with 4,6,8 etc also would lead to pairs(I think this point is lacking in all the answers and comments so far for this question) .

ManishKr
  • 211
  • 2
  • 9
0

From what you have posted, your code is good. You have a map that contains the count for each values found.

Now, you just need to print what you want,

for(Integer i : hmap.keySet()){
    Integer cnt = hmap.get(i);
    if(cnt > 1)} //or == 2
        System.out.println(i + " -> " + cnt);
    }
}

And you have an nice little input for each pair (with the count if there is more), just update your output.

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • Yah, now i got the logic and its working fine for me. Thanks a lot :) – Kaustav Nov 29 '16 at 07:46
  • @Kaustav You are welcome, in the futur, think about giving every information from the start ;) ;) If you found a answer that match you needs, don't hesitate to accept it (just belove the vote system of the answer) to close the question. And upvote every answers that you found usefull. – AxelH Nov 29 '16 at 07:51
  • I will .Thanks :) – Kaustav Nov 29 '16 at 07:53
  • @Kaustav I was just reading your other question (although it was deleted) and reviewing your activity, I noticed you have still not marked this answer as Accepted. – Patrick Parker Dec 05 '16 at 16:53