Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何计算ArrayList中的非重复(仅出现一次)字符串?_Java_String_Arraylist_Unique_Non Repetitive - Fatal编程技术网

Java:如何计算ArrayList中的非重复(仅出现一次)字符串?

Java:如何计算ArrayList中的非重复(仅出现一次)字符串?,java,string,arraylist,unique,non-repetitive,Java,String,Arraylist,Unique,Non Repetitive,我试图找到在ArrayList中只出现一次的字符串数 我实现了多少(最好是以尽可能最佳的时间复杂度) 以下是我的方法: public static int countNonRepeats(WordStream words) { ArrayList<String> list = new ArrayList<String>(); for (String i : words) { list.add(i); } Collecti

我试图找到在
ArrayList
中只出现一次的字符串数

我实现了多少(最好是以尽可能最佳的时间复杂度)

以下是我的方法:

  public static int countNonRepeats(WordStream words) {

    ArrayList<String> list = new ArrayList<String>();
    for (String i : words) {
      list.add(i);
    }

    Collections.sort(list);

    for (int i = 1; i < list.size(); i++) {
      if (list.get(i).equals(list.get(i - 1))) {
        list.remove(list.get(i));
        list.remove(list.get(i - 1));
      }
    }

    System.out.println(list);

    return list.size();
  }
publicstaticint countNonRepeats(字流字){
ArrayList=新建ArrayList();
for(字符串i:单词){
列表.添加(i);
}
集合。排序(列表);
对于(int i=1;i

为什么不删除
list.get(i)
list.get(i-1)

这里有一个简单的建议:

  • 首先,按字母数字顺序对数组排序
  • 使用循环进行迭代,
    if(!list.get(i).equals(list.get(i+1)))→ 唯一的
  • 如果发现重复项,则递增
    i
    ,直到到达不同的字符串

  • 这将具有排序算法的复杂性,因为步骤2+3应该是O(n)

    这里有一个简单的建议:

  • 首先,按字母数字顺序对数组排序
  • 使用循环进行迭代,
    if(!list.get(i).equals(list.get(i+1)))→ 唯一的
  • 如果发现重复项,则递增
    i
    ,直到到达不同的字符串

  • 这将具有排序算法的复杂性,因为步骤2+3应该是O(n)

    是否有使用
    数组列表的特定需要?通过使用
    哈希集
    ,您可以轻松地完成此操作

    以下是代码片段:

    public static void main (String[] args) {
        String[] words = {"foo","bar","foo","fo","of","bar","of","ba","of","ab"};
        Set<String> set = new HashSet<>();
        Set<String> common = new HashSet<>();
        for (String i : words) {
            if(!set.add(i)) {
                common.add(i);
            }
        }
    
        System.out.println(set.size() - common.size());
    }
    
    以下是修改后的代码:

    public static int countNonRepeats(WordStream words) {
        Set<String> set = new HashSet<>();
        Set<String> common = new HashSet<>();
        for (String i : words) {
            if(!set.add(i)) {
                common.add(i);
            }
        }
    
        return (set.size() - common.size());
    }
    
    publicstaticint countNonRepeats(字流字){
    Set=newhashset();
    Set common=new HashSet();
    for(字符串i:单词){
    如果(!set.add(i)){
    共同.添加(i);
    }
    }
    返回(set.size()-common.size());
    }
    
    是否有使用
    阵列列表的特定需要?通过使用
    哈希集
    ,您可以轻松地完成此操作

    以下是代码片段:

    public static void main (String[] args) {
        String[] words = {"foo","bar","foo","fo","of","bar","of","ba","of","ab"};
        Set<String> set = new HashSet<>();
        Set<String> common = new HashSet<>();
        for (String i : words) {
            if(!set.add(i)) {
                common.add(i);
            }
        }
    
        System.out.println(set.size() - common.size());
    }
    
    以下是修改后的代码:

    public static int countNonRepeats(WordStream words) {
        Set<String> set = new HashSet<>();
        Set<String> common = new HashSet<>();
        for (String i : words) {
            if(!set.add(i)) {
                common.add(i);
            }
        }
    
        return (set.size() - common.size());
    }
    
    publicstaticint countNonRepeats(字流字){
    Set=newhashset();
    Set common=new HashSet();
    for(字符串i:单词){
    如果(!set.add(i)){
    共同.添加(i);
    }
    }
    返回(set.size()-common.size());
    }
    
    您可以使用hashmap来实现这一点。通过这种方法,我们可以计算所有单词的出现次数,
    如果我们只对唯一的单词感兴趣,则访问count=1的元素
    HashMap
    -键表示arraylist中的字符串,整数表示出现的计数。

            ArrayList<String> list = new ArrayList<String>();
            HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
    
            for (int i = 0; i < list.size(); i++) {
    
                String key = list.get(i);
    
                if (hashMap.get(key) != null) {
                    int value = hashMap.get(key);
                    value++;
                    hashMap.put(key, value);
                } else {
                        hashMap.put(key, 1);
                }
    
            }
            int uniqueCount = 0;
            Iterator it = hashMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                if ((int) pair.getValue() == 1)
                    uniqueCount++;
            }
            System.out.println(uniqueCount);
    
    ArrayList list=new ArrayList();
    HashMap HashMap=新的HashMap();
    对于(int i=0;i
    您可以使用hashmap来实现这一点。通过这种方法,我们可以计算所有单词的出现次数,
    如果我们只对唯一的单词感兴趣,则访问count=1的元素
    HashMap
    -键表示arraylist中的字符串,整数表示出现的计数。

            ArrayList<String> list = new ArrayList<String>();
            HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
    
            for (int i = 0; i < list.size(); i++) {
    
                String key = list.get(i);
    
                if (hashMap.get(key) != null) {
                    int value = hashMap.get(key);
                    value++;
                    hashMap.put(key, value);
                } else {
                        hashMap.put(key, 1);
                }
    
            }
            int uniqueCount = 0;
            Iterator it = hashMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                if ((int) pair.getValue() == 1)
                    uniqueCount++;
            }
            System.out.println(uniqueCount);
    
    ArrayList list=new ArrayList();
    HashMap HashMap=新的HashMap();
    对于(int i=0;i
    不需要排序。 更好的方法是使用两个HashSet,一个用于保持重复,另一个用于非重复单词。由于HashSet在内部使用HashMap,理想情况下contains、get、put操作的复杂性为o(1)。因此,该方法的总体复杂性为o(n)

    public static int countNonRepeats(列出单词){
    Set nonRepeating=新HashSet();
    Set repeating=新HashSet();
    for(字符串i:单词){
    如果(!repeating.contains(i)){
    if(非重复包含(i)){
    重复。添加(i);
    不重复。移除(i);
    }否则{
    不重复。添加(i);
    }
    }
    }
    System.out.println(nonRepeating.size());
    返回非重复的.size();
    }
    
    不需要排序。 更好的方法是使用两个HashSet,一个用于保持重复,另一个用于非重复单词。由于HashSet在内部使用HashMap,理想情况下contains、get、put操作的复杂性为o(1)。因此,该方法的总体复杂性为o(n)

    public static int countNonRepeats(列出单词){
    Set nonRepeating=新HashSet();
    Set repeating=新HashSet();
    for(字符串i:单词){
    如果(!repeating.contains(i)){
    if(非重复包含(i)){
    重复。添加(i);
    诺尔