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).

// element occurrences in list
static <A> int countOccurrences(Iterable<A> c, A x) {
  int n = 0;
  if (c != null) for (O o : c)
    if (eq(x, o))
      ++n;
  ret n;
}

static <A> int countOccurrences(A x, Iterable<A> c) {
  ret countOccurrences(c, x);
}

// string occurrences in string (not counting overlapping)
static int countOccurrences(S s, S x) {
  int i = 0, nx = l(x), count = 0;
  if (nx == 0) ret 0;
  while ((i = indexOf(s, x, i)) >= 0) {
    ++count;
    i += nx;
  }
  ret count;
}

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: 494 / 632
Version history: 7 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1013683 - countOccurrencesIC (ignore case)
#1028950 - countOccurrences_optimizedForZero - now just a synonym of countOccurrences