Advertisement
Hiteshw11

Common Elements In Two Lists

Nov 23rd, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.85 KB | Source Code | 0 0
  1. /* Write a Dart function that takes two lists and returns a list of elements that are present in both lists. */
  2.  
  3. void main() {
  4.   List<int> list1= [1,2,3,4];
  5.   List<int> list2=  [3,4,5,6];
  6.   print(commoncheck(list1,list2));
  7. }
  8.  
  9. List commoncheck(list1,list2)
  10. {
  11. Set<int> s1 = Set.from(list1);
  12. List <int> list11 = s1.toList();
  13.  
  14. Set<int> s2 = Set.from(list2);
  15. List <int> list22 = s2.toList();
  16.  
  17. List <int> list3 = [];
  18.  
  19. list3.addAll(list11);
  20. list3.addAll(list22);
  21.  
  22. Set <int> s3 = Set.from(list3);
  23. List list33 = s3.toList();
  24.  
  25. Map<int,int> m1= {};
  26.  
  27. for(int i=0;i<list33.length;i++)
  28. {
  29.  
  30.  m1[list33[i]]=0;
  31. }
  32.  
  33.  
  34. for(int i=0;i<list3.length;i++)
  35. {
  36.    m1[list3[i]]=m1[list3[i]]!+1;
  37. }
  38.  
  39. List<int> repeat =[];
  40.  
  41. for(MapEntry <int,int> e in m1.entries)
  42. {
  43.     if(e.value>1)
  44.      {
  45.       repeat.add(e.key);
  46.      }
  47. }
  48.  
  49. print(m1);
  50.  
  51. return repeat;
  52.  
  53. }
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement