Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Deduplicated List Method

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Deduplicated List Method

    ref. https://forum.ninjatrader.com/forum/...oubles-in-list

    PHP Code:
    
            List<double> DeDuplList(List<double> _List)
            {                      
                if (_List.Count < 1)
                    return new List<double>();
                
                for (int i = 0 ; i < _List.Count -1 ; i++)
                {      
                    double st1  = _List[i+1];
                    double nd2  = _List[i];
                    
                    if (st1 == nd2)
                    {
                        _List.Remove(st1);
                    }
                }
                
                return _List;
            }&#8203; 
    

    Tested ok:
    Test your C# code online with .NET Fiddle code editor.

    PHP Code:
    
    using System;
    using System.Collections.Generic;
                        
    public class Program
    {
        public static void Main()
        {
            List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DuplList List Items indices: " + i + " DuplList List Items: " + duplList[i]);
            }
            
            List<double> DeDuplList(List<double> _List)
            {                      
                if (_List.Count < 1)
                    return new List<double>();
                
                for (int i = 0 ; i < _List.Count -1 ; i++)
                {      
                    double st1  = _List[i+1];
                    double nd2  = _List[i];
                    
                    if (st1 == nd2)
                    {
                        _List.Remove(st1);
                    }
                }
                
                return _List;
            }
            
            duplList = DeDuplList(duplList);
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DeDuplList List Items indices: " + i + " DeDuplList List Items: " + duplList[i]);
            }
        }
    }&#8203; 
    

    Output:


    DuplList List Items indices: 0 DuplList List Items: 1.1
    DuplList List Items indices: 1 DuplList List Items: 2.2
    DuplList List Items indices: 2 DuplList List Items: 2.2
    DuplList List Items indices: 3 DuplList List Items: 3.3
    DuplList List Items indices: 4 DuplList List Items: 4.4
    DuplList List Items indices: 5 DuplList List Items: 5.5
    DuplList List Items indices: 6 DuplList List Items: 5.5
    DuplList List Items indices: 7 DuplList List Items: 6.6
    DuplList List Items indices: 8 DuplList List Items: 7.7

    DeDuplList List Items indices: 0 DeDuplList List Items: 1.1
    DeDuplList List Items indices: 1 DeDuplList List Items: 2.2
    DeDuplList List Items indices: 2 DeDuplList List Items: 3.3
    DeDuplList List Items indices: 3 DeDuplList List Items: 4.4
    DeDuplList List Items indices: 4 DeDuplList List Items: 5.5
    DeDuplList List Items indices: 5 DeDuplList List Items: 6.6
    DeDuplList List Items indices: 6 DeDuplList List Items: 7.7​
    ref. Callable list method
    In C#, How do I call a function that is returning a list? static void Main(string[] args) { List&lt;string&gt; range = new List&lt;string&gt;(); range.ForEach(item ...

    Return empty list
    I have a c# function that reads file locations from a Datatable, and returns a List with all the file lcoations to the calling method. In the Catch block, I want to return an empty list with a fal...
    Last edited by PaulMohn; 09-19-2024, 11:25 AM.

    #2
    Initial version without List Method:

    PHP Code:
    
    using System;
    using System.Collections.Generic;
                        
    public class Program
    {
        public static void Main()
        {
            List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DuplList List Items indices: " + i + " DuplList List Items: " + duplList[i]);
            }
                
            for (int i = 0 ; i < duplList.Count -1 ; i++)
            {      
                double st1  = duplList[i+1];
                double nd2  = duplList[i];
    
                if (st1 == nd2)
                {
                    duplList.Remove(st1);
                }
            }
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DeDuplList List Items indices: " + i + " DeDuplList List Items: " + duplList[i]);
            }
        }
    }&#8203; 
    
    Output:


    DuplList List Items indices: 0 DuplList List Items: 1.1
    DuplList List Items indices: 1 DuplList List Items: 2.2
    DuplList List Items indices: 2 DuplList List Items: 2.2
    DuplList List Items indices: 3 DuplList List Items: 3.3
    DuplList List Items indices: 4 DuplList List Items: 4.4
    DuplList List Items indices: 5 DuplList List Items: 5.5
    DuplList List Items indices: 6 DuplList List Items: 5.5
    DuplList List Items indices: 7 DuplList List Items: 6.6
    DuplList List Items indices: 8 DuplList List Items: 7.7

    DeDuplList List Items indices: 0 DeDuplList List Items: 1.1
    DeDuplList List Items indices: 1 DeDuplList List Items: 2.2
    DeDuplList List Items indices: 2 DeDuplList List Items: 3.3
    DeDuplList List Items indices: 3 DeDuplList List Items: 4.4
    DeDuplList List Items indices: 4 DeDuplList List Items: 5.5
    DeDuplList List Items indices: 5 DeDuplList List Items: 6.6
    DeDuplList List Items indices: 6 DeDuplList List Items: 7.7​
    Tested ok:
    Test your C# code online with .NET Fiddle code editor.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    576 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    334 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    553 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    551 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X