如何打印不重复的字符串?

时间:2022-08-01 21:42:16

I tried to print the string without duplicate but i not getting the proper output and here I exposed my code snippets.

我试图打印字符串没有重复但我没有得到正确的输出,在这里我暴露了我的代码片段。

class Duplicatestring
{
public static void main(String [] args)
    {
    String word = "";
            String[]  ip ={"mani" ," manivannan","raghv ","mani"};  
    for(int i =0; i<ip.length; i++)
    {
        for(int j = i+1; j<=ip.length; j++)
        {
                if(ip[i].equals(ip[j])){ 
            word = word+ip[i];

        }
        }
        System.out.println(word);

    }       

    }
}

And one more thing is I don't want use the collections that is my task and pleas give any proper solution for this.

还有一件事是我不想使用我的任务的集合,请求为此提供任何适当的解决方案。

Example:

例:

Input -> {mani, manivanna,raghv, mani};
output -> {mani, manivanna,raghv}              

5 个解决方案

#1


1  

If you don't want to use collections then I assume it's a homework, so I don't want to provide you a full solution, but I'll guide you.

如果你不想使用收藏品,那么我认为它是一个家庭作业,所以我不想为你提供完整的解决方案,但我会指导你。

You can have a helper array of the size of the original array. Now you write two nested loops and for each word, if you find a duplicate, you mark the helper array with 1.

您可以拥有一个原始数组大小的辅助数组。现在你编写两个嵌套循环,对于每个单词,如果找到重复,则用1标记辅助数组。

After this procedure you'll have something like this in the helper array:

在这个过程之后,你将在helper数组中有这样的东西:

[0,0,0,1]

Now you iterate on the arrays in parallel and print the element only if the corresponding index in the helper array is 0.

现在,您并行迭代数组并仅在辅助数组中的相应索引为0时才打印元素。

Solution is O(n2).

解是O(n2)。

#2


1  

Your loop is incorrect. To solve the problem, you can use a Set to eliminate duplicated words.

你的循环不正确。要解决此问题,可以使用Set来消除重复的单词。

If the problem must be solved by O(n^2) loops, the following code will work:

如果问题必须通过O(n ^ 2)循环解决,则以下代码将起作用:

public class Duplicatestring {

    public static void main(String[] args) {

        String[] ip = { "mani", " manivannan", "raghv ", "mani" };
        for (int i = 0; i < ip.length; i++) {

            boolean duplicated = false;

            //search back to check if a same word already exists
            for (int j = i - 1; j >= 0; j--) {
                if(ip[i].equals(ip[j])) {
                    duplicated = true;
                    break;
                }
            }
            if(!duplicated) {
                System.out.println(ip[i]);
            }
        }

    }
}

#3


0  

if you want to remove the duplicate from the array call the below method and pass the array has the duplicate values.. it will return you the array with non-duplicate values..

如果你想从数组中删除副本,请调用以下方法并传递数组具有重复值..它将返回具有非重复值的数组。

call method here
ip = removeDuplicates(ip);

public static int[] removeDuplicates(int[] arr){
    //dest array index
    int destination = 0;

    //source array index
    int source = 0;

    int currentValue = arr[0];

    int[] whitelist = new int[arr.length];
    whitelist[destination] = currentValue;
    while(source < arr.length){

        if(currentValue == arr[source]){
            source++;
        } else {
            currentValue = arr[source];
            destination++;
            source++;
            whitelist[destination] = currentValue;
        }
    }
    int[] returnList = new int[++destination];

    for(int i = 0; i < destination; i++){
        returnList[i] = whitelist[i];
    }

    return returnList;
}

it will return you the non duplicates values array..!!

它将返回非重复值数组.. !!

#4


0  

u may try this:

你可以试试这个:

public class HelloWorld{

 public static void main(String []args){
    String[] names = {"john", "adam", "will", "lee", "john", "seon", "lee"};
    String s;
    for (int i = 0; names.length > i; i ++) {
        s = names[i];
        if (!isDuplicate(s, i, names)) {
            System.out.println(s);
        }
    }
 }
 private static boolean isDuplicate(String item, int j, String[] items) {
    boolean duplicate = Boolean.FALSE;
     for (int i = 0; j > i; i++) {
         if (items[i].equals(item)) {
             duplicate = Boolean.TRUE;
             break;
         }
     }
     return duplicate;
 }
}

output

产量

john
adam
will
lee
seon

#5


-1  

if string order does not matter for you, you can also use the TreeSet.. check the below code.. simple and sweet.

如果字符串顺序对你没有关系,你也可以使用TreeSet ..检查下面的代码..简单而甜蜜。

import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

public class MyArrayDuplicates { 
    public static void main(String a[]){
        String[] strArr = {"one","two","three","four","four","five"};
        //convert string array to list
        List<String> tmpList = Arrays.asList(strArr);
        //create a treeset with the list, which eliminates duplicates
        TreeSet<String> unique = new TreeSet<String>(tmpList);
        System.out.println(unique);
        System.out.println();
        Iterator<Integer> iterator = unique.iterator();

       // Displaying the Tree set data
       while (iterator.hasNext()) {
           System.out.print(iterator.next() + " ");
       }
    }
}

it will print as -

它将打印为 -

[five, four, one, three, two]

five
four
one
three
two

#1


1  

If you don't want to use collections then I assume it's a homework, so I don't want to provide you a full solution, but I'll guide you.

如果你不想使用收藏品,那么我认为它是一个家庭作业,所以我不想为你提供完整的解决方案,但我会指导你。

You can have a helper array of the size of the original array. Now you write two nested loops and for each word, if you find a duplicate, you mark the helper array with 1.

您可以拥有一个原始数组大小的辅助数组。现在你编写两个嵌套循环,对于每个单词,如果找到重复,则用1标记辅助数组。

After this procedure you'll have something like this in the helper array:

在这个过程之后,你将在helper数组中有这样的东西:

[0,0,0,1]

Now you iterate on the arrays in parallel and print the element only if the corresponding index in the helper array is 0.

现在,您并行迭代数组并仅在辅助数组中的相应索引为0时才打印元素。

Solution is O(n2).

解是O(n2)。

#2


1  

Your loop is incorrect. To solve the problem, you can use a Set to eliminate duplicated words.

你的循环不正确。要解决此问题,可以使用Set来消除重复的单词。

If the problem must be solved by O(n^2) loops, the following code will work:

如果问题必须通过O(n ^ 2)循环解决,则以下代码将起作用:

public class Duplicatestring {

    public static void main(String[] args) {

        String[] ip = { "mani", " manivannan", "raghv ", "mani" };
        for (int i = 0; i < ip.length; i++) {

            boolean duplicated = false;

            //search back to check if a same word already exists
            for (int j = i - 1; j >= 0; j--) {
                if(ip[i].equals(ip[j])) {
                    duplicated = true;
                    break;
                }
            }
            if(!duplicated) {
                System.out.println(ip[i]);
            }
        }

    }
}

#3


0  

if you want to remove the duplicate from the array call the below method and pass the array has the duplicate values.. it will return you the array with non-duplicate values..

如果你想从数组中删除副本,请调用以下方法并传递数组具有重复值..它将返回具有非重复值的数组。

call method here
ip = removeDuplicates(ip);

public static int[] removeDuplicates(int[] arr){
    //dest array index
    int destination = 0;

    //source array index
    int source = 0;

    int currentValue = arr[0];

    int[] whitelist = new int[arr.length];
    whitelist[destination] = currentValue;
    while(source < arr.length){

        if(currentValue == arr[source]){
            source++;
        } else {
            currentValue = arr[source];
            destination++;
            source++;
            whitelist[destination] = currentValue;
        }
    }
    int[] returnList = new int[++destination];

    for(int i = 0; i < destination; i++){
        returnList[i] = whitelist[i];
    }

    return returnList;
}

it will return you the non duplicates values array..!!

它将返回非重复值数组.. !!

#4


0  

u may try this:

你可以试试这个:

public class HelloWorld{

 public static void main(String []args){
    String[] names = {"john", "adam", "will", "lee", "john", "seon", "lee"};
    String s;
    for (int i = 0; names.length > i; i ++) {
        s = names[i];
        if (!isDuplicate(s, i, names)) {
            System.out.println(s);
        }
    }
 }
 private static boolean isDuplicate(String item, int j, String[] items) {
    boolean duplicate = Boolean.FALSE;
     for (int i = 0; j > i; i++) {
         if (items[i].equals(item)) {
             duplicate = Boolean.TRUE;
             break;
         }
     }
     return duplicate;
 }
}

output

产量

john
adam
will
lee
seon

#5


-1  

if string order does not matter for you, you can also use the TreeSet.. check the below code.. simple and sweet.

如果字符串顺序对你没有关系,你也可以使用TreeSet ..检查下面的代码..简单而甜蜜。

import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

public class MyArrayDuplicates { 
    public static void main(String a[]){
        String[] strArr = {"one","two","three","four","four","five"};
        //convert string array to list
        List<String> tmpList = Arrays.asList(strArr);
        //create a treeset with the list, which eliminates duplicates
        TreeSet<String> unique = new TreeSet<String>(tmpList);
        System.out.println(unique);
        System.out.println();
        Iterator<Integer> iterator = unique.iterator();

       // Displaying the Tree set data
       while (iterator.hasNext()) {
           System.out.print(iterator.next() + " ");
       }
    }
}

it will print as -

它将打印为 -

[five, four, one, three, two]

five
four
one
three
two