Write code to output the no. of times each number appears in two unsorted arrays

Given two strings of unsorted integers write code to output the no. of times each number appears in both arrays. In the sample below number 2 appears 3 times
Sample Arrays:
array #1 [2,21,4,56,12,10,8,2,12,10,21]
aray #2 [12,8,2,10,56,12,8,8,12,4,12]

Showing Answers 1 - 12 of 12 Answers

preet

  • Sep 19th, 2017
 

Code
  1. public class RepeationTimes {

  2.  

  3.         public static void main(String[] args) {

  4.                 @SuppressWarnings("unused")

  5.                 int num1[]= new int[] {2,21,4,56,12,10,8,2,12,10,21};

  6.                

  7.                 @SuppressWarnings("unused")

  8.                 int num2[]=new int[]{12,8,2,10,56,12,8,8,12,4,12};

  9.                 int c=0;

  10.                

  11.                 for (int i=0;i<num1.length;i++){

  12.                         for (int j=0;j<num2.length;j++){

  13.                                 if (num1[i]==num2[j]){

  14.                                         c++;

  15.                                 }

  16.                                

  17.                         }

  18.                         System.out.println(num1[i]+ " "+" "+"Repeated "+c+" times");

  19.                         c=0;

  20.                 }

  21.  

  22.        

  23.         }

  24. }

  25.  

  Was this answer useful?  Yes

Ashwary Naveen

  • Jan 15th, 2018
 

Ans

Code
  1. package temp;

  2.  

  3. public class Exp {

  4.          public static void main(String[] args) {

  5.                  int num1[]= new int[] {2,21,4,56,12,10,8,2,12,10,21};

  6.          int num2[]=new int[]{12,8,2,10,56,12,8,8,12,4,12};

  7.          int max = num1[0];

  8.          for(int i=0;i<num1.length-1;i++){

  9.                  if (max < num1[i+1]){

  10.                          max = num1[i+1];

  11.                  }

  12.          }

  13.          for(int i=0;i<num2.length-1;i++){

  14.                  if (max < num2[i+1]){

  15.                          max = num2[i+1];

  16.                  }

  17.          }

  18.          int out[] = new int[max+1];

  19.          for(int i=1;i<=out.length;i++){

  20.                  for(int j=0;j<num1.length;j++){

  21.                          if(i==num1[j])

  22.                                  out[i] = out[i] + 1;

  23.                          if(i==num2[j])

  24.                                  out[i] = out[i] + 1;

  25.                  }

  26.          }

  27.          for(int i=1;i<=max;i++){

  28.                  if(out[i]>0)

  29.          System.out.println("num "+i+ " comes "+out[i]+" times.");

  30.          }

  31. }

  32. }

  33.  

  Was this answer useful?  Yes

Rita

  • May 10th, 2018
 

Write code to output the no. of times each number appears in two unsorted arrays

Code
  1. public class ArrayCountRepNo {

  2.         public static void main(String args[]) {

  3.                 int[] num1 = { 2, 21, 4, 56, 12, 10, 8, 2, 12, 10, 21 };

  4.                 int[] num2 = { 12, 8, 2, 10, 56, 12, 8, 8, 12, 4, 12 };

  5.                 int counter = 0;

  6.                 for (int i = 0; i < num1.length; i++) {

  7.                         for (int k = 0; k < num1.length; k++) {

  8.                                 if (num1[i] == num1[k]) {

  9.                                         counter++;

  10.                                 }

  11.                         }

  12.                         for (int j = 0; j < num2.length; j++) {

  13.                                 if (num1[i] == num2[j]) {

  14.                                         counter++;

  15.                                 }

  16.  

  17.                         }

  18.                         System.out.println(num1[i] + " " + " " + "Repeated " + counter

  19.                                         + " times");

  20.                         counter = 0;

  21.  

  22.                 }

  23.  

  24.         }

  25. }

  Was this answer useful?  Yes

Shabana Khan

  • May 18th, 2020
 

2 --> 3
21 --> 2
4 --> 2
56 --> 2
12 --> 6
10 --> 3
8 --> 4

Code
  1. public class CountElementsInBothArrays {

  2.  

  3.         public static void main(String[] args) {

  4.                 int num1[]= new int[] {2,21,4,56,12,10,8,2,12,10,21};

  5.                  int num2[]=new int[]{12,8,2,10,56,12,8,8,12,4,12};

  6.                  countElements(num1,num2);

  7.                

  8.                

  9.  

  10.         }

  11.         public static void countElements(int[] num1,int[] num2)

  12.         {

  13.                 int[] num=new int[num1.length+num2.length];

  14.                 System.arraycopy(num1, 0, num, 0, num1.length);

  15.                 System.arraycopy(num2, 0, num, num1.length, num2.length);

  16.                

  17.                 Map<Integer,Integer> mp = new LinkedHashMap<>();

  18.                 for(int i=0;i<num.length;i++)

  19.                 {

  20.                         mp.put(num[i], (mp.getOrDefault(num[i], 0))+1);

  21.                 }

  22.                 for(Map.Entry<Integer, Integer> entry : mp.entrySet())

  23.                 {

  24.                         System.out.println(entry.getKey()+" --> "+entry.getValue());

  25.                 }

  26.         }

  27.  

  28. }

  29.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions