SlideShare a Scribd company logo
Write a method countUnique that takes a List of integers as a parameter and returns the number
of unique integer values in the list. Use a Set as auxiliary storage to help you solve this problem.
For example, if a list contains the values [3, 7, 3, -1, 2, 3, 7, 2, 15, 15], your method should
return 5. The empty list contains 0 unique values.
Solution
package arraylistexample;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class ARRAYLISTEXAMPLE {
public static void main(String[] args) {
List list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter some numbers..");
System.out.println("Press any non integer(Either Float or character ) for ending the input");
while (scanner.hasNextInt()) {
int i = scanner.nextInt();
list.add(i);
}
int k = alok.countUnique(list);
System.out.print(" Total number of unique elements Are : t" + k);
}
}
class alok {
public static int countUnique(List lst) {
Set set = new HashSet<>(lst);
System.out.println("Set values .....");
set.stream().forEach((temp) -> {
System.out.print(temp + "t");
});
int n = set.size();
return n;
}
}

More Related Content

Similar to Write a method countUnique that takes a List of integers as a parame.pdf

I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdfI am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
RAJATCHUGH12
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
import java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdfimport java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdf
shettysachin2005
 
I really need help with the code for this in Java.Set operations u.pdf
I really need help with the code for this in Java.Set operations u.pdfI really need help with the code for this in Java.Set operations u.pdf
I really need help with the code for this in Java.Set operations u.pdf
dbrienmhompsonkath75
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
fashioncollection2
 

Similar to Write a method countUnique that takes a List of integers as a parame.pdf (13)

Computer java programs
Computer java programsComputer java programs
Computer java programs
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
 
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdfI am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
 
22.ppt
22.ppt22.ppt
22.ppt
 
Lec2
Lec2Lec2
Lec2
 
Java -lec-5
Java -lec-5Java -lec-5
Java -lec-5
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
import java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdfimport java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdf
 
Array data structure
Array data structureArray data structure
Array data structure
 
I really need help with the code for this in Java.Set operations u.pdf
I really need help with the code for this in Java.Set operations u.pdfI really need help with the code for this in Java.Set operations u.pdf
I really need help with the code for this in Java.Set operations u.pdf
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 

More from MALASADHNANI

File yuan.h#include iostream#include fstream#include .pdf
File yuan.h#include iostream#include fstream#include .pdfFile yuan.h#include iostream#include fstream#include .pdf
File yuan.h#include iostream#include fstream#include .pdf
MALASADHNANI
 
Discus the development of the fraud examinerforensic accounting pro.pdf
Discus the development of the fraud examinerforensic accounting pro.pdfDiscus the development of the fraud examinerforensic accounting pro.pdf
Discus the development of the fraud examinerforensic accounting pro.pdf
MALASADHNANI
 
Define a class named Doctor whose objects are records for clinic’s d.pdf
Define a class named Doctor whose objects are records for clinic’s d.pdfDefine a class named Doctor whose objects are records for clinic’s d.pdf
Define a class named Doctor whose objects are records for clinic’s d.pdf
MALASADHNANI
 
Briefly discuss five criteria used to classify an organism as a multi.pdf
Briefly discuss five criteria used to classify an organism as a multi.pdfBriefly discuss five criteria used to classify an organism as a multi.pdf
Briefly discuss five criteria used to classify an organism as a multi.pdf
MALASADHNANI
 

More from MALASADHNANI (20)

I have two statistics questions, I was wondering if you could answer.pdf
I have two statistics questions, I was wondering if you could answer.pdfI have two statistics questions, I was wondering if you could answer.pdf
I have two statistics questions, I was wondering if you could answer.pdf
 
How does increased transcription (mutation in the G1-S- checkpoint p.pdf
How does increased transcription (mutation in the G1-S- checkpoint p.pdfHow does increased transcription (mutation in the G1-S- checkpoint p.pdf
How does increased transcription (mutation in the G1-S- checkpoint p.pdf
 
How many different phases would a 4-PSK modulated signal with the fo.pdf
How many different phases would a 4-PSK modulated signal with the fo.pdfHow many different phases would a 4-PSK modulated signal with the fo.pdf
How many different phases would a 4-PSK modulated signal with the fo.pdf
 
how to get pure culture from mix culture. Microbiologyhow to.pdf
how to get pure culture from mix culture. Microbiologyhow to.pdfhow to get pure culture from mix culture. Microbiologyhow to.pdf
how to get pure culture from mix culture. Microbiologyhow to.pdf
 
from mendels time to the 1950s, what were the three model organism.pdf
from mendels time to the 1950s, what were the three model organism.pdffrom mendels time to the 1950s, what were the three model organism.pdf
from mendels time to the 1950s, what were the three model organism.pdf
 
File yuan.h#include iostream#include fstream#include .pdf
File yuan.h#include iostream#include fstream#include .pdfFile yuan.h#include iostream#include fstream#include .pdf
File yuan.h#include iostream#include fstream#include .pdf
 
diastole; systole; atriaThe cardiac cycle refers to the rhythmic rel.pdf
diastole; systole; atriaThe cardiac cycle refers to the rhythmic rel.pdfdiastole; systole; atriaThe cardiac cycle refers to the rhythmic rel.pdf
diastole; systole; atriaThe cardiac cycle refers to the rhythmic rel.pdf
 
Discuss how human activities influence some aspect of its life ando.pdf
Discuss how  human activities influence some aspect of its life ando.pdfDiscuss how  human activities influence some aspect of its life ando.pdf
Discuss how human activities influence some aspect of its life ando.pdf
 
Early in my auditing career, I always wondered how do I know when .pdf
Early in my auditing career, I always wondered how do I know when .pdfEarly in my auditing career, I always wondered how do I know when .pdf
Early in my auditing career, I always wondered how do I know when .pdf
 
Discus the development of the fraud examinerforensic accounting pro.pdf
Discus the development of the fraud examinerforensic accounting pro.pdfDiscus the development of the fraud examinerforensic accounting pro.pdf
Discus the development of the fraud examinerforensic accounting pro.pdf
 
Could a cell survive by having just peripheral proteins on it’s memb.pdf
Could a cell survive by having just peripheral proteins on it’s memb.pdfCould a cell survive by having just peripheral proteins on it’s memb.pdf
Could a cell survive by having just peripheral proteins on it’s memb.pdf
 
Define a class named Doctor whose objects are records for clinic’s d.pdf
Define a class named Doctor whose objects are records for clinic’s d.pdfDefine a class named Doctor whose objects are records for clinic’s d.pdf
Define a class named Doctor whose objects are records for clinic’s d.pdf
 
cell nuclei experiment1. What is function of SDS and O.5M NaCl in .pdf
cell nuclei experiment1. What is function of SDS and O.5M NaCl in .pdfcell nuclei experiment1. What is function of SDS and O.5M NaCl in .pdf
cell nuclei experiment1. What is function of SDS and O.5M NaCl in .pdf
 
C++ Sorting The parameter to the following two recursive routines is.pdf
C++ Sorting The parameter to the following two recursive routines is.pdfC++ Sorting The parameter to the following two recursive routines is.pdf
C++ Sorting The parameter to the following two recursive routines is.pdf
 
Briefly discuss five criteria used to classify an organism as a multi.pdf
Briefly discuss five criteria used to classify an organism as a multi.pdfBriefly discuss five criteria used to classify an organism as a multi.pdf
Briefly discuss five criteria used to classify an organism as a multi.pdf
 
Which of the following statements about detritivores is true they a.pdf
Which of the following statements about detritivores is true  they a.pdfWhich of the following statements about detritivores is true  they a.pdf
Which of the following statements about detritivores is true they a.pdf
 
What is the role of anaerobic cellular respiration in the nitrogen c.pdf
What is the role of anaerobic cellular respiration in the nitrogen c.pdfWhat is the role of anaerobic cellular respiration in the nitrogen c.pdf
What is the role of anaerobic cellular respiration in the nitrogen c.pdf
 
What is on Important difference between genetic dissection and genomi.pdf
What is on Important difference between genetic dissection and genomi.pdfWhat is on Important difference between genetic dissection and genomi.pdf
What is on Important difference between genetic dissection and genomi.pdf
 
Ralph has seven different colors of leftover paint. Since the.pdf
Ralph has seven different colors of leftover paint. Since the.pdfRalph has seven different colors of leftover paint. Since the.pdf
Ralph has seven different colors of leftover paint. Since the.pdf
 
The following figure shows two possible groups of species on which to.pdf
The following figure shows two possible groups of species on which to.pdfThe following figure shows two possible groups of species on which to.pdf
The following figure shows two possible groups of species on which to.pdf
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 

Write a method countUnique that takes a List of integers as a parame.pdf

  • 1. Write a method countUnique that takes a List of integers as a parameter and returns the number of unique integer values in the list. Use a Set as auxiliary storage to help you solve this problem. For example, if a list contains the values [3, 7, 3, -1, 2, 3, 7, 2, 15, 15], your method should return 5. The empty list contains 0 unique values. Solution package arraylistexample; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class ARRAYLISTEXAMPLE { public static void main(String[] args) { List list = new ArrayList<>(); Scanner scanner = new Scanner(System.in); System.out.print("Enter some numbers.."); System.out.println("Press any non integer(Either Float or character ) for ending the input"); while (scanner.hasNextInt()) { int i = scanner.nextInt(); list.add(i); } int k = alok.countUnique(list); System.out.print(" Total number of unique elements Are : t" + k); } } class alok { public static int countUnique(List lst) { Set set = new HashSet<>(lst); System.out.println("Set values ....."); set.stream().forEach((temp) -> { System.out.print(temp + "t"); }); int n = set.size();