Thursday, June 6

Removing duplicate values from the array list using java -- it contains String compression,List,Hashset techniques

Hi All,


Here I Write a program for Removing duplicate values from the array list using three techniques
1. Normal String compression for finding duplicates from array list 

2. List using to compare string and find & remove duplicates from array list 

3. Hash set method to find and remove the duplicates from the array list.


import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

public class removeDuplicateas {

    public static void main(String args[]) {
        String[] value = new String[]{"S", "K", "I", "T", "E", "C", "H", "", "-", "a", "b", "a", "b"};

        /* Normal  string method for find and display the duplicates from array*/
        if (checkDuplicated_withNormal(value)) {
            System.out.println("Array contains duplicate values \n");
        }
         /* Hashset method for find and remove the duplicates from array*/
        if (checkDuplicated_withSet(value)) {
            System.out.println(" \n");
        }

        /* List method using find and remove the duplicates from array*/

        listCheck();

    }

    public static void listCheck() {

        List<String> skilist = (List<String>) Arrays.asList("macos", "windows", "linux", "Android", "", "Boss", "macos", "windows");

        LinkedHashSet<String> lsToset = new LinkedHashSet<String>(skilist);
        List<String> lsWoDuplicates = new ArrayList<String>(lsToset);
        System.out.println("ArrayList size without duplicate: " + lsToset.size());
        System.out.println("ArrayList after removing duplicates in order: " + lsWoDuplicates);
    }

    //check duplicated value using noraml string check method
    private static boolean checkDuplicated_withNormal(String[] value) {
        for (int i = 0; i < value.length; i++) {
            String ValueCheck = value[i];
            if (ValueCheck == null || ValueCheck.equals("")) {
                continue; //empty value ignore condition
            }
            for (int j = 0; j < value.length; j++) {
                if (i == j) {
                    continue;
                }
                String Valuematch = value[j];
                if (ValueCheck.equals(Valuematch)) {
                    return true;
                }
            }

        }
        return false;

    }

    //check Without Duplicate values
    private static boolean checkDuplicated_withSet(String[] values) {
        Set<String> sValueSet = new HashSet<String>();
        for (String temp : values) {
            if (sValueSet.contains(temp)) {
                return true;
            } else if (!temp.equals("")) {
                sValueSet.add(temp);
            }
            System.out.println("Without Duplicate values " + sValueSet.toString());
        }
        return false;
    }
}


output:

~~~~~~~


Array contains duplicate values

Without Duplicate values [S]
Without Duplicate values [S, K]
Without Duplicate values [S, I, K]
Without Duplicate values [T, S, I, K]
Without Duplicate values [E, T, S, I, K]
Without Duplicate values [E, T, S, C, I, K]
Without Duplicate values [E, T, S, C, H, I, K]
Without Duplicate values [E, T, S, C, H, I, K]
Without Duplicate values [E, T, S, C, H, I, -, K]
Without Duplicate values [E, T, S, a, C, H, I, -, K]
Without Duplicate values [E, T, b, S, a, C, H, I, -, K]


ArrayList size without duplicate: 6
ArrayList after removing duplicates in order: [macos, windows, linux, Android, , Boss]

No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...