Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #1008879 // countOccurrences

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2482L/16K).

1  
// element occurrences in list
2  
static <A> int countOccurrences(Iterable<A> c, A x) {
3  
  int n = 0;
4  
  if (c != null) for (O o : c)
5  
    if (eq(x, o))
6  
      ++n;
7  
  ret n;
8  
}
9  
10  
static <A> int countOccurrences(A x, Iterable<A> c) {
11  
  ret countOccurrences(c, x);
12  
}
13  
14  
// string occurrences in string (not counting overlapping)
15  
static int countOccurrences(S s, S x) {
16  
  int i = 0, nx = l(x), count = 0;
17  
  if (nx == 0) ret 0;
18  
  while ((i = indexOf(s, x, i)) >= 0) {
19  
    ++count;
20  
    i += nx;
21  
  }
22  
  ret count;
23  
}

Author comment

Began life as a copy of #1008859

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1008879
Snippet name: countOccurrences
Eternal ID of this version: #1008879/8
Text MD5: 9cf9fa636c5d7159f8109e69a1620744
Transpilation MD5: 6d2a352fc2001f64f954515a526f32f9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-12 18:01:53
Source code size: 519 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 493 / 632
Version history: 7 change(s)
Referenced in: [show references]