Remove the duplicate pair from List

how to remove duplicate key value pair in javascript
remove duplicate objects from array typescript
remove duplicates
remove duplicates from array
remove duplicates algorithm
remove duplicates from array typescript
remove duplicates from array javascript
remove duplicates from string

i was working on one issue i am not getting the expected output.

A String array "F1" has got names of Facebook users and their friend association.

For example, if we write: U1,U2 it implies that U1 is a friend of U2. This also implies that U2 is a friend of U1. Write a program which will read "F1" and remove duplicates and write all the unique pairs to "F2". But, before removing duplicates

Input String => ["U1,U2","U3,U4","U2,U1","U1,U5"]

Output String => ["U1,U2","U1,U5","U3,U4"]

public static void main(String args[]) {

    List test = new ArrayList();
    List<String> list = new ArrayList();
    list.add("U1,U2");
    list.add("U3,U4");
    list.add("U2,U1");
    list.add("U1,U5");
    Collections.sort(list);

    for (String str : list) {
        String i1 = str.substring(0, 2);
        String i2 = str.substring(3, 5);
         System.out.println(i2);
         if (!i1.equals(i2)) {
         test.add(str);
         }
        if (!(str.contains(i1) && str.contains(i2)) || !(str.contains(i2) && str.contains(i1))) {
            System.out.println(str);
        }


    }

}

}

This is how it is done ;)

class Program
{
    static void Main(string[] args)
    {
        List<string> test = new List<String>();
        List<String> list = new List<String>();
        List<String> list1 = new List<String>();
        List<String> outputList = new List<String>();
        list.Add("MARY,JOE");
        list.Add("A,B");
        list.Add("B, A");
        list.Add("3");
        list.Add("3");
        list.Add("3,3");
        list.Add("3,3");
        var aa = compareFriends(list);
        Console.ReadLine();
    }

    public static List<string> compareFriends(List<string> allfrndsList)
    {
        var frndsList  = allfrndsList.Distinct().ToList();
        var outputList = new List<string>(frndsList);
        var listCount = frndsList.Count();
        var startIndex = 1;

        foreach (var friend in frndsList)
        {
            friend.Replace(" ","");
            var str = friend.Split(',');
            var i1 = str.FirstOrDefault();
            var i2 = str.LastOrDefault();
            for (var index = startIndex; index < listCount; index++)
            {
                var innerStr = frndsList[index].Replace(" ","").Split(',');
                var temp1 = innerStr.FirstOrDefault();
                var temp2 = innerStr.LastOrDefault();
                if (innerStr.Length != 1 && str.Length!=1)
                {
                    if (i1.Equals(temp2) && i2.Equals(temp1))
                    {
                        outputList.Remove(friend);
                    }
                }
            }
            startIndex++;
        }
        return outputList;
    }
}

How to Remove Duplicates from ArrayList in Java, Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java. Examples: Input: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5,  Remove any duplicates from a List: mylist = ["a", "b", "a", "c", "c"] mylist = list (dict.fromkeys (mylist)) print(mylist) Try it Yourself ».

Make a key of every pair, where the first value is smaller than the second value. ("U1,U2" and "U2,U2" will both result in "U1,U2"). Add these keys to a Set (a set removes duplicates for you). In the end you have a unique set of friend relationships.

Set<String> f2=new HashSet<>();

for (String str : list) {
    String[] users=str.split(",");
    String i1 = users[0];
    String i2 = users[1];
    String key=i1.compareTo(i2)>0?i2+","+i1:i1+","+i2;
    f2.add(key);
}
System.out.println(f2);

How to remove duplicates from an array of objects using JavaScript , This approach will remove the duplicate objects as only one of each object of the original array will get assigned to the same index. Example: Display the list of array objects first_page How to create an array with key value pairs in PHP? I don't think removepairs is a good name because it doesn't actually remove anything from the list; it returns a new list with the pairs removed. Therefore, I would call it get_singles . You shouldn't use list as a variable name because it shadows the built-in type.

    List test = new ArrayList();
    List<String> list = new ArrayList();
    list.add("JOE,MARY");
    list.add("A,B");
    Set<String> f2 = new HashSet<>();

    for (String str : list) {
        String[] users = str.split(",");
        String i1 = users[0];
        String i2 = users[1];
        String key = i1.compareTo(i2) > 0 ? i2 + "," + i1 : i1 + "," + i2;
        f2.add(key);
    }
    Iterator val = f2.iterator();
    while (val.hasNext()) {
        test.add(val.next());
    }
    Collections.sort(test);
    System.out.println("test" + test);

How to remove duplicate pairs in a List, I got a List with pairs of integers. How do I remove pairs if they're duplicates? Distinct wont work cause the pair could be (2, 1) instead of (1, 2). My list looks like   Python Remove Duplicates From List Now you can see in the output duplicate items has been removed from the list. Using Not In On The List In this way, we first create an empty list then start a loop that goes through the list, append the first occurrence of the element in new list and ignore all the other occurrences of that particular element.

In C#:

class Program
{
    static void Main(string[] args)
    {
        List<string> list1 = new List<string>();

        list1.Add("U1,U2");
        list1.Add("U3,U4");
        list1.Add("U2,U1");
        list1.Add("U1,U5");

        List<string> list2 = new List<string>();

        foreach (string s in list1)
        {
            var list3 = s.Split(',');
            Array.Sort(list3);
            list2.Add(list3.FirstOrDefault() + ',' + list3.LastOrDefault());

        }

        list2.Sort();
        var list4 = list2.Distinct().ToList();

        Console.WriteLine("Input string:");
        foreach (string s in list1)
            Console.WriteLine(s);

        Console.WriteLine("\nOutput string:");
        foreach (string s in list4)
            Console.WriteLine(s);
        Console.Read();
    }
}

How to Remove Duplicates from a List in Python, Removing duplicates from a list is a common operation in Python. Dictionaries map keys to values and create a pair that can be used to store  After these operations, the list contains a duplicate. The Remove method is used to remove the first instance of the duplicate item, and the contents are displayed. The Remove method always removes the first instance it encounters.

We can look at it as an undirected graph You have to remove duplicate paths Since it's undirected, "U1, U2" and "U2, U1" are the same path Therefore, if path U1, U2 already exist, then U2, U1 is duplicate, so remove it.

Any of the suggestions in the comments will yield a viable solution

C# Remove Duplicates From List, Eliminate duplicate elements from a List with Distinct, HashSet, or for-loops in an optimal way. Remove duplicates. In a parallel universe, all things are duplicated. We can follow the following steps to remove the duplicate values. Select the required cell range i.e. A1:A16 in our example. Click the ‘ Data ‘ tab. Click on ‘ Remove Duplicates ‘ option as shown in figure 2 below.

Python Remove Duplicates From List, Remove duplicates. A list contains duplicate elements. How can we remove them ? Some approaches may lead to the elements becoming reordered, but this is  By Greg Harvey . You can use Excel’s Eliminate Duplicates feature to quickly find and remove duplicate records from a list (or rows from a table). This is a great Excel feature especially when you’re dealing with a really large data list in which several different people do the data entry and which should not have any duplicate records (such as client lists, personnel files, and the like).

Python: Remove duplicates from a list of lists, Write a Python program to remove duplicates from a list of lists. Sample Solution:- Python Code: import itertools num = [[10, 20], [40], [30, 56, 25]  Python Remove Duplicates from a List The job is simple. We need to take a list, with duplicate elements in it and generate another list which only contains the element without the duplicates in them.

Python: Find duplicates in a list with frequency count & index , Step 1: Get duplicate elements in a list with a frequency count Keep pairs whose value is greater than 1 i.e. only duplicate elements from list. Remove elements from dictionary whose value is 1, i.e. non duplicate items.

Comments
  • you can try to split it into arrays, and then sort that array and then check whether it is equal to any of the entries already present. Other thing that you can try out is add the ascii value of all the characters of the string, and then check if the same added up ascii is already present.
  • You can also try to check if two strings contain the same characters - stackoverflow.com/questions/3985328/…
  • i1.equals(i2) would only be true when someone is their own friend. In contrast, str.contains(i1) and str.contains(i2) are always true when i1 and i2 are substrings of str.
  • Save a string concatenation when the original string does already have the right order: String key = i1.compareTo(i2) <= 0? str: i2 + "," + i1;