Monday 4 March 2019

How to get the count of elements in an array? Duplicate Elements in array? Count of duplicate elements in array?Collection Interview Questions

This is one of the mostly asked interview question.

package CountElements;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class findduplicate {
    public static void main (String args[]) {
        String str2 = "findduplicateelementsinarray";
        String[] ary = str2.split("");
     ArrayList<String> list = new ArrayList<String>(Arrays.asList(ary));
 //Set does not allow duplicate elements
     Set<String> str1 = new HashSet<String>();{
     for ( String str21 : list) {
         if (str1.add(str21) == false) {
             System.out.println(str21 + " occurs: " + Collections.frequency(list, str21) +": is the Duplicate Element");
         }else if (Collections.frequency(list, str21) < 2) {
             System.out.println(str21 + " occurs: " + Collections.frequency(list, str21));
         }
     }
   
}}
}

*************************************************

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions





Note: Ask me questions in the comment section, or any doubts on the above post.

Click To Learn most asked Interview question
Java questions:
Most asked Java Interview Questions

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...